2013-08-07 12:20:04 +04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/windows.h>
|
|
|
|
#include <winpr/synch.h>
|
|
|
|
#include <winpr/sysinfo.h>
|
|
|
|
#include <winpr/thread.h>
|
|
|
|
#include <winpr/interlocked.h>
|
|
|
|
|
|
|
|
#define TEST_SYNC_CRITICAL_TEST1_RUNTIME_MS 500
|
|
|
|
#define TEST_SYNC_CRITICAL_TEST1_RUNS 4
|
|
|
|
|
2018-03-07 14:03:10 +03:00
|
|
|
static CRITICAL_SECTION critical;
|
|
|
|
static LONG gTestValueVulnerable = 0;
|
|
|
|
static LONG gTestValueSerialized = 0;
|
2013-08-07 12:20:04 +04:00
|
|
|
|
2018-03-07 14:03:10 +03:00
|
|
|
static BOOL TestSynchCritical_TriggerAndCheckRaceCondition(HANDLE OwningThread, LONG RecursionCount)
|
2013-08-07 12:20:04 +04:00
|
|
|
{
|
|
|
|
/* if called unprotected this will hopefully trigger a race condition ... */
|
|
|
|
gTestValueVulnerable++;
|
|
|
|
|
|
|
|
if (critical.OwningThread != OwningThread)
|
|
|
|
{
|
|
|
|
printf("CriticalSection failure: OwningThread is invalid\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (critical.RecursionCount != RecursionCount)
|
|
|
|
{
|
|
|
|
printf("CriticalSection failure: RecursionCount is invalid\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ... which we try to detect using the serialized counter */
|
|
|
|
if (gTestValueVulnerable != InterlockedIncrement(&gTestValueSerialized))
|
|
|
|
{
|
|
|
|
printf("CriticalSection failure: Data corruption detected\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
/* this thread function shall increment the global dwTestValue until the PBOOL passsed in arg is
|
|
|
|
* FALSE */
|
2018-03-07 14:03:10 +03:00
|
|
|
static DWORD WINAPI TestSynchCritical_Test1(LPVOID arg)
|
2013-08-07 12:20:04 +04:00
|
|
|
{
|
|
|
|
int i, j, rc;
|
2019-11-06 17:24:51 +03:00
|
|
|
HANDLE hThread = (HANDLE)(ULONG_PTR)GetCurrentThreadId();
|
2013-08-07 12:20:04 +04:00
|
|
|
|
|
|
|
PBOOL pbContinueRunning = (PBOOL)arg;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
while (*pbContinueRunning)
|
2013-08-07 12:20:04 +04:00
|
|
|
{
|
|
|
|
EnterCriticalSection(&critical);
|
|
|
|
|
|
|
|
rc = 1;
|
|
|
|
|
|
|
|
if (!TestSynchCritical_TriggerAndCheckRaceCondition(hThread, rc))
|
2018-03-07 16:47:06 +03:00
|
|
|
return 1;
|
2013-08-07 12:20:04 +04:00
|
|
|
|
|
|
|
/* add some random recursion level */
|
2019-11-06 17:24:51 +03:00
|
|
|
j = rand() % 5;
|
|
|
|
for (i = 0; i < j; i++)
|
2013-08-07 12:20:04 +04:00
|
|
|
{
|
|
|
|
if (!TestSynchCritical_TriggerAndCheckRaceCondition(hThread, rc++))
|
2018-03-07 16:47:06 +03:00
|
|
|
return 2;
|
2013-08-07 12:20:04 +04:00
|
|
|
EnterCriticalSection(&critical);
|
|
|
|
}
|
2019-11-06 17:24:51 +03:00
|
|
|
for (i = 0; i < j; i++)
|
2013-08-07 12:20:04 +04:00
|
|
|
{
|
|
|
|
if (!TestSynchCritical_TriggerAndCheckRaceCondition(hThread, rc--))
|
2018-03-07 16:47:06 +03:00
|
|
|
return 2;
|
2013-08-07 12:20:04 +04:00
|
|
|
LeaveCriticalSection(&critical);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!TestSynchCritical_TriggerAndCheckRaceCondition(hThread, rc))
|
2018-03-07 16:47:06 +03:00
|
|
|
return 3;
|
2013-08-07 12:20:04 +04:00
|
|
|
|
|
|
|
LeaveCriticalSection(&critical);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
/* this thread function tries to call TryEnterCriticalSection while the main thread holds the lock
|
|
|
|
*/
|
2018-03-07 14:03:10 +03:00
|
|
|
static DWORD WINAPI TestSynchCritical_Test2(LPVOID arg)
|
2013-08-07 12:20:04 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
if (TryEnterCriticalSection(&critical) == TRUE)
|
2013-08-07 12:20:04 +04:00
|
|
|
{
|
|
|
|
LeaveCriticalSection(&critical);
|
2018-03-07 14:03:10 +03:00
|
|
|
return 1;
|
2013-08-07 12:20:04 +04:00
|
|
|
}
|
2018-03-07 14:03:10 +03:00
|
|
|
return 0;
|
2013-08-07 12:20:04 +04:00
|
|
|
}
|
|
|
|
|
2018-03-07 14:03:10 +03:00
|
|
|
static DWORD WINAPI TestSynchCritical_Main(LPVOID arg)
|
2013-08-07 12:20:04 +04:00
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
SYSTEM_INFO sysinfo;
|
|
|
|
DWORD dwPreviousSpinCount;
|
|
|
|
DWORD dwSpinCount;
|
|
|
|
DWORD dwSpinCountExpected;
|
|
|
|
HANDLE hMainThread;
|
|
|
|
HANDLE* hThreads;
|
|
|
|
HANDLE hThread;
|
|
|
|
DWORD dwThreadCount;
|
|
|
|
DWORD dwThreadExitCode;
|
|
|
|
BOOL bTest1Running;
|
|
|
|
|
|
|
|
PBOOL pbThreadTerminated = (PBOOL)arg;
|
|
|
|
|
|
|
|
GetNativeSystemInfo(&sysinfo);
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
hMainThread = (HANDLE)(ULONG_PTR)GetCurrentThreadId();
|
2013-08-07 12:20:04 +04:00
|
|
|
|
|
|
|
/**
|
2019-11-06 17:24:51 +03:00
|
|
|
* Test SpinCount in SetCriticalSectionSpinCount, InitializeCriticalSectionEx and
|
|
|
|
* InitializeCriticalSectionAndSpinCount SpinCount must be forced to be zero on on uniprocessor
|
|
|
|
* systems and on systems where WINPR_CRITICAL_SECTION_DISABLE_SPINCOUNT is defined
|
2013-08-07 12:20:04 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
dwSpinCount = 100;
|
|
|
|
InitializeCriticalSectionEx(&critical, dwSpinCount, 0);
|
2019-11-06 17:24:51 +03:00
|
|
|
while (--dwSpinCount)
|
2013-08-07 12:20:04 +04:00
|
|
|
{
|
|
|
|
dwPreviousSpinCount = SetCriticalSectionSpinCount(&critical, dwSpinCount);
|
|
|
|
dwSpinCountExpected = 0;
|
|
|
|
#if !defined(WINPR_CRITICAL_SECTION_DISABLE_SPINCOUNT)
|
|
|
|
if (sysinfo.dwNumberOfProcessors > 1)
|
2019-11-06 17:24:51 +03:00
|
|
|
dwSpinCountExpected = dwSpinCount + 1;
|
2013-08-07 12:20:04 +04:00
|
|
|
#endif
|
|
|
|
if (dwPreviousSpinCount != dwSpinCountExpected)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
printf("CriticalSection failure: SetCriticalSectionSpinCount returned %" PRIu32
|
|
|
|
" (expected: %" PRIu32 ")\n",
|
|
|
|
dwPreviousSpinCount, dwSpinCountExpected);
|
2013-08-07 12:20:04 +04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
DeleteCriticalSection(&critical);
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (dwSpinCount % 2 == 0)
|
2013-08-07 12:20:04 +04:00
|
|
|
InitializeCriticalSectionAndSpinCount(&critical, dwSpinCount);
|
|
|
|
else
|
|
|
|
InitializeCriticalSectionEx(&critical, dwSpinCount, 0);
|
|
|
|
}
|
|
|
|
DeleteCriticalSection(&critical);
|
|
|
|
|
|
|
|
/**
|
2019-11-06 17:24:51 +03:00
|
|
|
* Test single-threaded recursive
|
|
|
|
* TryEnterCriticalSection/EnterCriticalSection/LeaveCriticalSection
|
2013-08-07 12:20:04 +04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
InitializeCriticalSection(&critical);
|
|
|
|
|
2014-05-22 22:42:05 +04:00
|
|
|
for (i = 0; i < 1000; i++)
|
2013-08-07 12:20:04 +04:00
|
|
|
{
|
|
|
|
if (critical.RecursionCount != i)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
printf("CriticalSection failure: RecursionCount field is %" PRId32 " instead of %d.\n",
|
|
|
|
critical.RecursionCount, i);
|
2013-08-07 12:20:04 +04:00
|
|
|
goto fail;
|
|
|
|
}
|
2019-11-06 17:24:51 +03:00
|
|
|
if (i % 2 == 0)
|
2013-08-07 12:20:04 +04:00
|
|
|
{
|
|
|
|
EnterCriticalSection(&critical);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (TryEnterCriticalSection(&critical) == FALSE)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
printf("CriticalSection failure: TryEnterCriticalSection failed where it should "
|
|
|
|
"not.\n");
|
2013-08-07 12:20:04 +04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (critical.OwningThread != hMainThread)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
printf("CriticalSection failure: Could not verify section ownership (loop index=%d).\n",
|
|
|
|
i);
|
2013-08-07 12:20:04 +04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (--i >= 0)
|
|
|
|
{
|
|
|
|
LeaveCriticalSection(&critical);
|
|
|
|
if (critical.RecursionCount != i)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
printf("CriticalSection failure: RecursionCount field is %" PRId32 " instead of %d.\n",
|
|
|
|
critical.RecursionCount, i);
|
2013-08-07 12:20:04 +04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (critical.OwningThread != (HANDLE)(i ? hMainThread : NULL))
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
printf("CriticalSection failure: Could not verify section ownership (loop index=%d).\n",
|
|
|
|
i);
|
2013-08-07 12:20:04 +04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DeleteCriticalSection(&critical);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test using multiple threads modifying the same value
|
|
|
|
*/
|
|
|
|
|
|
|
|
dwThreadCount = sysinfo.dwNumberOfProcessors > 1 ? sysinfo.dwNumberOfProcessors : 2;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
hThreads = (HANDLE*)calloc(dwThreadCount, sizeof(HANDLE));
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!hThreads)
|
|
|
|
{
|
|
|
|
printf("Problem allocating memory\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
2013-08-07 12:20:04 +04:00
|
|
|
|
2014-05-22 22:42:05 +04:00
|
|
|
for (j = 0; j < TEST_SYNC_CRITICAL_TEST1_RUNS; j++)
|
2013-08-07 12:20:04 +04:00
|
|
|
{
|
|
|
|
dwSpinCount = j * 1000;
|
|
|
|
InitializeCriticalSectionAndSpinCount(&critical, dwSpinCount);
|
|
|
|
|
|
|
|
gTestValueVulnerable = 0;
|
|
|
|
gTestValueSerialized = 0;
|
|
|
|
|
|
|
|
/* the TestSynchCritical_Test1 threads shall run until bTest1Running is FALSE */
|
|
|
|
bTest1Running = TRUE;
|
2019-11-06 17:24:51 +03:00
|
|
|
for (i = 0; i < (int)dwThreadCount; i++)
|
2015-05-05 14:55:48 +03:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!(hThreads[i] =
|
|
|
|
CreateThread(NULL, 0, TestSynchCritical_Test1, &bTest1Running, 0, NULL)))
|
2015-05-05 14:55:48 +03:00
|
|
|
{
|
|
|
|
printf("CriticalSection failure: Failed to create test_1 thread #%d\n", i);
|
|
|
|
goto fail;
|
|
|
|
}
|
2013-08-07 12:20:04 +04:00
|
|
|
}
|
2014-05-22 22:42:05 +04:00
|
|
|
|
2013-08-07 12:20:04 +04:00
|
|
|
/* let it run for TEST_SYNC_CRITICAL_TEST1_RUNTIME_MS ... */
|
|
|
|
Sleep(TEST_SYNC_CRITICAL_TEST1_RUNTIME_MS);
|
|
|
|
bTest1Running = FALSE;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
for (i = 0; i < (int)dwThreadCount; i++)
|
2013-08-07 12:20:04 +04:00
|
|
|
{
|
|
|
|
if (WaitForSingleObject(hThreads[i], INFINITE) != WAIT_OBJECT_0)
|
|
|
|
{
|
|
|
|
printf("CriticalSection failure: Failed to wait for thread #%d\n", i);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
GetExitCodeThread(hThreads[i], &dwThreadExitCode);
|
2019-11-06 17:24:51 +03:00
|
|
|
if (dwThreadExitCode != 0)
|
2013-08-07 12:20:04 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
printf("CriticalSection failure: Thread #%d returned error code %" PRIu32 "\n", i,
|
|
|
|
dwThreadExitCode);
|
2013-08-07 12:20:04 +04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
CloseHandle(hThreads[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gTestValueVulnerable != gTestValueSerialized)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
printf("CriticalSection failure: unexpected test value %" PRId32 " (expected %" PRId32
|
|
|
|
")\n",
|
|
|
|
gTestValueVulnerable, gTestValueSerialized);
|
2013-08-07 12:20:04 +04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
DeleteCriticalSection(&critical);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(hThreads);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TryEnterCriticalSection in thread must fail if we hold the lock in the main thread
|
|
|
|
*/
|
|
|
|
|
|
|
|
InitializeCriticalSection(&critical);
|
|
|
|
|
|
|
|
if (TryEnterCriticalSection(&critical) == FALSE)
|
|
|
|
{
|
|
|
|
printf("CriticalSection failure: TryEnterCriticalSection unexpectedly failed.\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
/* This thread tries to call TryEnterCriticalSection which must fail */
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!(hThread = CreateThread(NULL, 0, TestSynchCritical_Test2, NULL, 0, NULL)))
|
2015-05-05 14:55:48 +03:00
|
|
|
{
|
|
|
|
printf("CriticalSection failure: Failed to create test_2 thread\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
2013-08-07 12:20:04 +04:00
|
|
|
if (WaitForSingleObject(hThread, INFINITE) != WAIT_OBJECT_0)
|
|
|
|
{
|
|
|
|
printf("CriticalSection failure: Failed to wait for thread\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
GetExitCodeThread(hThread, &dwThreadExitCode);
|
2019-11-06 17:24:51 +03:00
|
|
|
if (dwThreadExitCode != 0)
|
2013-08-07 12:20:04 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
printf("CriticalSection failure: Thread returned error code %" PRIu32 "\n",
|
|
|
|
dwThreadExitCode);
|
2013-08-07 12:20:04 +04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
CloseHandle(hThread);
|
|
|
|
|
|
|
|
*pbThreadTerminated = TRUE; /* requ. for winpr issue, see below */
|
2018-03-07 16:47:06 +03:00
|
|
|
return 0;
|
2013-08-07 12:20:04 +04:00
|
|
|
|
|
|
|
fail:
|
|
|
|
*pbThreadTerminated = TRUE; /* requ. for winpr issue, see below */
|
2018-03-07 16:47:06 +03:00
|
|
|
return 1;
|
2013-08-07 12:20:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int TestSynchCritical(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
BOOL bThreadTerminated = FALSE;
|
|
|
|
HANDLE hThread;
|
|
|
|
DWORD dwThreadExitCode;
|
|
|
|
DWORD dwDeadLockDetectionTimeMs;
|
2014-05-22 22:42:05 +04:00
|
|
|
DWORD i;
|
2013-08-07 12:20:04 +04:00
|
|
|
|
2021-06-01 13:41:20 +03:00
|
|
|
WINPR_UNUSED(argc);
|
|
|
|
WINPR_UNUSED(argv);
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
dwDeadLockDetectionTimeMs =
|
2021-06-01 13:41:20 +03:00
|
|
|
6 * TEST_SYNC_CRITICAL_TEST1_RUNTIME_MS * TEST_SYNC_CRITICAL_TEST1_RUNS;
|
2013-08-07 12:20:04 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
printf("Deadlock will be assumed after %" PRIu32 " ms.\n", dwDeadLockDetectionTimeMs);
|
2013-08-07 12:20:04 +04:00
|
|
|
|
2018-03-07 14:03:10 +03:00
|
|
|
if (!(hThread = CreateThread(NULL, 0, TestSynchCritical_Main, &bThreadTerminated, 0, NULL)))
|
2015-05-05 14:55:48 +03:00
|
|
|
{
|
|
|
|
printf("CriticalSection failure: Failed to create main thread\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2013-08-07 12:20:04 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* We have to be able to detect dead locks in this test.
|
2019-11-06 17:24:51 +03:00
|
|
|
* At the time of writing winpr's WaitForSingleObject has not implemented timeout for thread
|
|
|
|
* wait
|
2013-08-07 12:20:04 +04:00
|
|
|
*
|
|
|
|
* Workaround checking the value of bThreadTerminated which is passed in the thread arg
|
|
|
|
*/
|
|
|
|
|
2014-05-22 22:42:05 +04:00
|
|
|
for (i = 0; i < dwDeadLockDetectionTimeMs; i += 100)
|
2013-08-07 12:20:04 +04:00
|
|
|
{
|
|
|
|
if (bThreadTerminated)
|
|
|
|
break;
|
2014-05-22 22:42:05 +04:00
|
|
|
|
2013-08-07 12:20:04 +04:00
|
|
|
Sleep(100);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bThreadTerminated)
|
|
|
|
{
|
|
|
|
printf("CriticalSection failure: Possible dead lock detected\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
GetExitCodeThread(hThread, &dwThreadExitCode);
|
|
|
|
CloseHandle(hThread);
|
|
|
|
|
2014-05-22 22:42:05 +04:00
|
|
|
if (dwThreadExitCode != 0)
|
2013-08-07 12:20:04 +04:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|