TestSynchCritical.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. #include <stdio.h>
  2. #include <winpr/crt.h>
  3. #include <winpr/windows.h>
  4. #include <winpr/synch.h>
  5. #include <winpr/sysinfo.h>
  6. #include <winpr/thread.h>
  7. #include <winpr/interlocked.h>
  8. #define TEST_SYNC_CRITICAL_TEST1_RUNTIME_MS 500
  9. #define TEST_SYNC_CRITICAL_TEST1_RUNS 4
  10. static CRITICAL_SECTION critical;
  11. static LONG gTestValueVulnerable = 0;
  12. static LONG gTestValueSerialized = 0;
  13. static BOOL TestSynchCritical_TriggerAndCheckRaceCondition(HANDLE OwningThread, LONG RecursionCount)
  14. {
  15. /* if called unprotected this will hopefully trigger a race condition ... */
  16. gTestValueVulnerable++;
  17. if (critical.OwningThread != OwningThread)
  18. {
  19. printf("CriticalSection failure: OwningThread is invalid\n");
  20. return FALSE;
  21. }
  22. if (critical.RecursionCount != RecursionCount)
  23. {
  24. printf("CriticalSection failure: RecursionCount is invalid\n");
  25. return FALSE;
  26. }
  27. /* ... which we try to detect using the serialized counter */
  28. if (gTestValueVulnerable != InterlockedIncrement(&gTestValueSerialized))
  29. {
  30. printf("CriticalSection failure: Data corruption detected\n");
  31. return FALSE;
  32. }
  33. return TRUE;
  34. }
  35. /* this thread function shall increment the global dwTestValue until the PBOOL passsed in arg is
  36. * FALSE */
  37. static DWORD WINAPI TestSynchCritical_Test1(LPVOID arg)
  38. {
  39. int i, j, rc;
  40. HANDLE hThread = (HANDLE)(ULONG_PTR)GetCurrentThreadId();
  41. PBOOL pbContinueRunning = (PBOOL)arg;
  42. while (*pbContinueRunning)
  43. {
  44. EnterCriticalSection(&critical);
  45. rc = 1;
  46. if (!TestSynchCritical_TriggerAndCheckRaceCondition(hThread, rc))
  47. return 1;
  48. /* add some random recursion level */
  49. j = rand() % 5;
  50. for (i = 0; i < j; i++)
  51. {
  52. if (!TestSynchCritical_TriggerAndCheckRaceCondition(hThread, rc++))
  53. return 2;
  54. EnterCriticalSection(&critical);
  55. }
  56. for (i = 0; i < j; i++)
  57. {
  58. if (!TestSynchCritical_TriggerAndCheckRaceCondition(hThread, rc--))
  59. return 2;
  60. LeaveCriticalSection(&critical);
  61. }
  62. if (!TestSynchCritical_TriggerAndCheckRaceCondition(hThread, rc))
  63. return 3;
  64. LeaveCriticalSection(&critical);
  65. }
  66. return 0;
  67. }
  68. /* this thread function tries to call TryEnterCriticalSection while the main thread holds the lock
  69. */
  70. static DWORD WINAPI TestSynchCritical_Test2(LPVOID arg)
  71. {
  72. if (TryEnterCriticalSection(&critical) == TRUE)
  73. {
  74. LeaveCriticalSection(&critical);
  75. return 1;
  76. }
  77. return 0;
  78. }
  79. static DWORD WINAPI TestSynchCritical_Main(LPVOID arg)
  80. {
  81. int i, j;
  82. SYSTEM_INFO sysinfo;
  83. DWORD dwPreviousSpinCount;
  84. DWORD dwSpinCount;
  85. DWORD dwSpinCountExpected;
  86. HANDLE hMainThread;
  87. HANDLE* hThreads;
  88. HANDLE hThread;
  89. DWORD dwThreadCount;
  90. DWORD dwThreadExitCode;
  91. BOOL bTest1Running;
  92. PBOOL pbThreadTerminated = (PBOOL)arg;
  93. GetNativeSystemInfo(&sysinfo);
  94. hMainThread = (HANDLE)(ULONG_PTR)GetCurrentThreadId();
  95. /**
  96. * Test SpinCount in SetCriticalSectionSpinCount, InitializeCriticalSectionEx and
  97. * InitializeCriticalSectionAndSpinCount SpinCount must be forced to be zero on on uniprocessor
  98. * systems and on systems where WINPR_CRITICAL_SECTION_DISABLE_SPINCOUNT is defined
  99. */
  100. dwSpinCount = 100;
  101. InitializeCriticalSectionEx(&critical, dwSpinCount, 0);
  102. while (--dwSpinCount)
  103. {
  104. dwPreviousSpinCount = SetCriticalSectionSpinCount(&critical, dwSpinCount);
  105. dwSpinCountExpected = 0;
  106. #if !defined(WINPR_CRITICAL_SECTION_DISABLE_SPINCOUNT)
  107. if (sysinfo.dwNumberOfProcessors > 1)
  108. dwSpinCountExpected = dwSpinCount + 1;
  109. #endif
  110. if (dwPreviousSpinCount != dwSpinCountExpected)
  111. {
  112. printf("CriticalSection failure: SetCriticalSectionSpinCount returned %" PRIu32
  113. " (expected: %" PRIu32 ")\n",
  114. dwPreviousSpinCount, dwSpinCountExpected);
  115. goto fail;
  116. }
  117. DeleteCriticalSection(&critical);
  118. if (dwSpinCount % 2 == 0)
  119. InitializeCriticalSectionAndSpinCount(&critical, dwSpinCount);
  120. else
  121. InitializeCriticalSectionEx(&critical, dwSpinCount, 0);
  122. }
  123. DeleteCriticalSection(&critical);
  124. /**
  125. * Test single-threaded recursive
  126. * TryEnterCriticalSection/EnterCriticalSection/LeaveCriticalSection
  127. *
  128. */
  129. InitializeCriticalSection(&critical);
  130. for (i = 0; i < 1000; i++)
  131. {
  132. if (critical.RecursionCount != i)
  133. {
  134. printf("CriticalSection failure: RecursionCount field is %" PRId32 " instead of %d.\n",
  135. critical.RecursionCount, i);
  136. goto fail;
  137. }
  138. if (i % 2 == 0)
  139. {
  140. EnterCriticalSection(&critical);
  141. }
  142. else
  143. {
  144. if (TryEnterCriticalSection(&critical) == FALSE)
  145. {
  146. printf("CriticalSection failure: TryEnterCriticalSection failed where it should "
  147. "not.\n");
  148. goto fail;
  149. }
  150. }
  151. if (critical.OwningThread != hMainThread)
  152. {
  153. printf("CriticalSection failure: Could not verify section ownership (loop index=%d).\n",
  154. i);
  155. goto fail;
  156. }
  157. }
  158. while (--i >= 0)
  159. {
  160. LeaveCriticalSection(&critical);
  161. if (critical.RecursionCount != i)
  162. {
  163. printf("CriticalSection failure: RecursionCount field is %" PRId32 " instead of %d.\n",
  164. critical.RecursionCount, i);
  165. goto fail;
  166. }
  167. if (critical.OwningThread != (HANDLE)(i ? hMainThread : NULL))
  168. {
  169. printf("CriticalSection failure: Could not verify section ownership (loop index=%d).\n",
  170. i);
  171. goto fail;
  172. }
  173. }
  174. DeleteCriticalSection(&critical);
  175. /**
  176. * Test using multiple threads modifying the same value
  177. */
  178. dwThreadCount = sysinfo.dwNumberOfProcessors > 1 ? sysinfo.dwNumberOfProcessors : 2;
  179. hThreads = (HANDLE*)calloc(dwThreadCount, sizeof(HANDLE));
  180. if (!hThreads)
  181. {
  182. printf("Problem allocating memory\n");
  183. goto fail;
  184. }
  185. for (j = 0; j < TEST_SYNC_CRITICAL_TEST1_RUNS; j++)
  186. {
  187. dwSpinCount = j * 1000;
  188. InitializeCriticalSectionAndSpinCount(&critical, dwSpinCount);
  189. gTestValueVulnerable = 0;
  190. gTestValueSerialized = 0;
  191. /* the TestSynchCritical_Test1 threads shall run until bTest1Running is FALSE */
  192. bTest1Running = TRUE;
  193. for (i = 0; i < (int)dwThreadCount; i++)
  194. {
  195. if (!(hThreads[i] =
  196. CreateThread(NULL, 0, TestSynchCritical_Test1, &bTest1Running, 0, NULL)))
  197. {
  198. printf("CriticalSection failure: Failed to create test_1 thread #%d\n", i);
  199. goto fail;
  200. }
  201. }
  202. /* let it run for TEST_SYNC_CRITICAL_TEST1_RUNTIME_MS ... */
  203. Sleep(TEST_SYNC_CRITICAL_TEST1_RUNTIME_MS);
  204. bTest1Running = FALSE;
  205. for (i = 0; i < (int)dwThreadCount; i++)
  206. {
  207. if (WaitForSingleObject(hThreads[i], INFINITE) != WAIT_OBJECT_0)
  208. {
  209. printf("CriticalSection failure: Failed to wait for thread #%d\n", i);
  210. goto fail;
  211. }
  212. GetExitCodeThread(hThreads[i], &dwThreadExitCode);
  213. if (dwThreadExitCode != 0)
  214. {
  215. printf("CriticalSection failure: Thread #%d returned error code %" PRIu32 "\n", i,
  216. dwThreadExitCode);
  217. goto fail;
  218. }
  219. CloseHandle(hThreads[i]);
  220. }
  221. if (gTestValueVulnerable != gTestValueSerialized)
  222. {
  223. printf("CriticalSection failure: unexpected test value %" PRId32 " (expected %" PRId32
  224. ")\n",
  225. gTestValueVulnerable, gTestValueSerialized);
  226. goto fail;
  227. }
  228. DeleteCriticalSection(&critical);
  229. }
  230. free(hThreads);
  231. /**
  232. * TryEnterCriticalSection in thread must fail if we hold the lock in the main thread
  233. */
  234. InitializeCriticalSection(&critical);
  235. if (TryEnterCriticalSection(&critical) == FALSE)
  236. {
  237. printf("CriticalSection failure: TryEnterCriticalSection unexpectedly failed.\n");
  238. goto fail;
  239. }
  240. /* This thread tries to call TryEnterCriticalSection which must fail */
  241. if (!(hThread = CreateThread(NULL, 0, TestSynchCritical_Test2, NULL, 0, NULL)))
  242. {
  243. printf("CriticalSection failure: Failed to create test_2 thread\n");
  244. goto fail;
  245. }
  246. if (WaitForSingleObject(hThread, INFINITE) != WAIT_OBJECT_0)
  247. {
  248. printf("CriticalSection failure: Failed to wait for thread\n");
  249. goto fail;
  250. }
  251. GetExitCodeThread(hThread, &dwThreadExitCode);
  252. if (dwThreadExitCode != 0)
  253. {
  254. printf("CriticalSection failure: Thread returned error code %" PRIu32 "\n",
  255. dwThreadExitCode);
  256. goto fail;
  257. }
  258. CloseHandle(hThread);
  259. *pbThreadTerminated = TRUE; /* requ. for winpr issue, see below */
  260. return 0;
  261. fail:
  262. *pbThreadTerminated = TRUE; /* requ. for winpr issue, see below */
  263. return 1;
  264. }
  265. int TestSynchCritical(int argc, char* argv[])
  266. {
  267. BOOL bThreadTerminated = FALSE;
  268. HANDLE hThread;
  269. DWORD dwThreadExitCode;
  270. DWORD dwDeadLockDetectionTimeMs;
  271. DWORD i;
  272. dwDeadLockDetectionTimeMs =
  273. 2 * TEST_SYNC_CRITICAL_TEST1_RUNTIME_MS * TEST_SYNC_CRITICAL_TEST1_RUNS;
  274. printf("Deadlock will be assumed after %" PRIu32 " ms.\n", dwDeadLockDetectionTimeMs);
  275. if (!(hThread = CreateThread(NULL, 0, TestSynchCritical_Main, &bThreadTerminated, 0, NULL)))
  276. {
  277. printf("CriticalSection failure: Failed to create main thread\n");
  278. return -1;
  279. }
  280. /**
  281. * We have to be able to detect dead locks in this test.
  282. * At the time of writing winpr's WaitForSingleObject has not implemented timeout for thread
  283. * wait
  284. *
  285. * Workaround checking the value of bThreadTerminated which is passed in the thread arg
  286. */
  287. for (i = 0; i < dwDeadLockDetectionTimeMs; i += 100)
  288. {
  289. if (bThreadTerminated)
  290. break;
  291. Sleep(100);
  292. }
  293. if (!bThreadTerminated)
  294. {
  295. printf("CriticalSection failure: Possible dead lock detected\n");
  296. return -1;
  297. }
  298. GetExitCodeThread(hThread, &dwThreadExitCode);
  299. CloseHandle(hThread);
  300. if (dwThreadExitCode != 0)
  301. {
  302. return -1;
  303. }
  304. return 0;
  305. }