summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/threading/NamedMutex/test1/namedmutex.cpp
blob: 3726214701c155590f0e28979e5984f195843370 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

// These test cases test named mutexes, including positive
// and negative cases, cross - thread and cross - process, mutual
// exclusion, abandon detection, etc.

#include <palsuite.h>

#ifndef _countof
#define _countof(a) (sizeof(a) / sizeof(a[0]))
#endif // !_countof

const char *const SessionPrefix = "Local\\";
const char *const GlobalPrefix = "Global\\";

const char *const NamePrefix = "paltest_namedmutex_test1_";
const char *const TempNamePrefix = "paltest_namedmutex_test1_temp_";
const char *const InvalidNamePrefix0 = "paltest\\namedmutex_";
const char *const InvalidNamePrefix1 = "paltest/namedmutex_";
const char *const ParentEventNamePrefix0 = "paltest_namedmutex_test1_pe0_";
const char *const ParentEventNamePrefix1 = "paltest_namedmutex_test1_pe1_";
const char *const ChildEventNamePrefix0 = "paltest_namedmutex_test1_ce0_";
const char *const ChildEventNamePrefix1 = "paltest_namedmutex_test1_ce1_";
const char *const ChildRunningEventNamePrefix = "paltest_namedmutex_test1_cr_";

const char *const GlobalShmFilePathPrefix = "/tmp/.dotnet/shm/global/";

#define MaxPathSize (200)
const DWORD PollLoopSleepMilliseconds = 100;
const DWORD FailTimeoutMilliseconds = 30000;
DWORD g_expectedTimeoutMilliseconds = 500;

bool g_isParent = true;
bool g_isStress = false;
char g_processPath[4096], g_processCommandLinePath[4096];
DWORD g_parentPid = static_cast<DWORD>(-1);

extern char *(*test_strcpy)(char *dest, const char *src);
extern int (*test_strcmp)(const char *s1, const char *s2);
extern size_t (*test_strlen)(const char *s);
extern int (*test_sprintf)(char *str, const char *format, ...);
extern int (*test_sscanf)(const char *str, const char *format, ...);
extern int(*test_close)(int fd);
extern int (*test_unlink)(const char *pathname);
extern unsigned int test_getpid();
extern int test_kill(unsigned int pid);

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Test helpers

extern bool TestFileExists(const char *path);
extern bool WriteHeaderInfo(const char *path, char sharedMemoryType, char version, int *fdRef);

#define TestAssert(expression) \
    do \
    { \
        if (!(expression)) \
        { \
            if (!g_isParent) \
            { \
                Trace("Child process: "); \
            } \
            Trace("'paltest_namedmutex_test1' failed at line %u. Expression: " #expression "\n", __LINE__); \
            fflush(stdout); \
            return false; \
        } \
    } while(false)

char *BuildName(const char *testName, char *buffer, const char *prefix0, const char *prefix1 = nullptr)
{
    size_t nameLength = 0;
    const char *prefixes[] = {prefix0, prefix1};
    for (int i = 0; i < 2; ++i)
    {
        const char *prefix = prefixes[i];
        if (prefix == nullptr)
        {
            break;
        }
        test_strcpy(&buffer[nameLength], prefix);
        nameLength += test_strlen(prefix);
    }

    if (g_isStress)
    {
        // Append the test name so that tests can run in parallel
        nameLength += test_sprintf(&buffer[nameLength], "%s", testName);
        buffer[nameLength++] = '_';
    }

    nameLength += test_sprintf(&buffer[nameLength], "%u", g_parentPid);
    return buffer;
}

char *BuildGlobalShmFilePath(const char *testName, char *buffer, const char *namePrefix)
{
    size_t pathLength = 0;
    test_strcpy(&buffer[pathLength], GlobalShmFilePathPrefix);
    pathLength += test_strlen(GlobalShmFilePathPrefix);
    test_strcpy(&buffer[pathLength], namePrefix);
    pathLength += test_strlen(namePrefix);

    if (g_isStress)
    {
        // Append the test name so that tests can run in parallel
        pathLength += test_sprintf(&buffer[pathLength], "%s", testName);
        buffer[pathLength++] = '_';
    }

    pathLength += test_sprintf(&buffer[pathLength], "%u", g_parentPid);
    return buffer;
}

class AutoCloseMutexHandle
{
private:
    HANDLE m_handle;

public:
    AutoCloseMutexHandle(HANDLE handle = nullptr) : m_handle(handle)
    {
    }

    ~AutoCloseMutexHandle()
    {
        Close();
    }

public:
    HANDLE GetHandle() const
    {
        return m_handle;
    }

    bool Release()
    {
        return !!ReleaseMutex(m_handle);
    }

    void Close()
    {
        if (m_handle != nullptr)
        {
            CloseHandle(m_handle);
            m_handle = nullptr;
        }
    }

    void Abandon()
    {
        // Don't close the handle
        m_handle = nullptr;
    }

    AutoCloseMutexHandle &operator =(HANDLE handle)
    {
        Close();
        m_handle = handle;
        return *this;
    }

    operator HANDLE() const
    {
        return m_handle;
    }

private:
    AutoCloseMutexHandle(const AutoCloseMutexHandle &other);
    AutoCloseMutexHandle(AutoCloseMutexHandle &&other);
    AutoCloseMutexHandle &operator =(const AutoCloseMutexHandle &other);
};

void TestCreateMutex(AutoCloseMutexHandle &m, const char *name, bool initiallyOwned = false)
{
    m.Close();
    m = CreateMutexA(nullptr, initiallyOwned, name);
}

HANDLE TestOpenMutex(const char *name)
{
    return OpenMutexA(SYNCHRONIZE, false, name);
}

bool StartProcess(const char *funcName)
{
    // Command line format: <processPath> <parentPid> <testFunctionName> [stress]

    size_t processCommandLinePathLength = 0;
    g_processCommandLinePath[processCommandLinePathLength++] = '\"';
    test_strcpy(&g_processCommandLinePath[processCommandLinePathLength], g_processPath);
    processCommandLinePathLength += test_strlen(g_processPath);
    g_processCommandLinePath[processCommandLinePathLength++] = '\"';
    g_processCommandLinePath[processCommandLinePathLength++] = ' ';
    processCommandLinePathLength += test_sprintf(&g_processCommandLinePath[processCommandLinePathLength], "%u", g_parentPid);
    g_processCommandLinePath[processCommandLinePathLength++] = ' ';
    test_strcpy(&g_processCommandLinePath[processCommandLinePathLength], funcName);
    processCommandLinePathLength += test_strlen(funcName);

    if (g_isStress)
    {
        test_strcpy(&g_processCommandLinePath[processCommandLinePathLength], " stress");
        processCommandLinePathLength += _countof("stress") - 1;
    }

    STARTUPINFO si;
    memset(&si, 0, sizeof(si));
    si.cb = sizeof(si);
    PROCESS_INFORMATION pi;
    memset(&pi, 0, sizeof(pi));
    if (!CreateProcessA(nullptr, g_processCommandLinePath, nullptr, nullptr, false, 0, nullptr, nullptr, &si, &pi))
    {
        return false;
    }
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);
    return true;
}

bool StartThread(LPTHREAD_START_ROUTINE func, void *arg = nullptr, HANDLE *threadHandleRef = nullptr)
{
    DWORD threadId;
    HANDLE handle = CreateThread(nullptr, 0, func, arg, 0, &threadId);
    if (handle != nullptr)
    {
        if (threadHandleRef == nullptr)
        {
            CloseHandle(handle);
        }
        else
        {
            *threadHandleRef = handle;
        }
        return true;
    }
    return false;
}

bool WaitForMutexToBeCreated(const char *testName, AutoCloseMutexHandle &m, const char *eventNamePrefix)
{
    char eventName[MaxPathSize];
    BuildName(testName, eventName, GlobalPrefix, eventNamePrefix);
    DWORD startTime = GetTickCount();
    while (true)
    {
        m = TestOpenMutex(eventName);
        if (m != nullptr)
        {
            return true;
        }
        if (GetTickCount() - startTime >= FailTimeoutMilliseconds)
        {
            return false;
        }
        Sleep(PollLoopSleepMilliseconds);
    }
}

// The following functions are used for parent/child tests, where the child runs in a separate thread or process. The tests are
// organized such that one the parent or child is ever running code, and they yield control and wait for the other. Since the
// named mutex is the only type of cross-process sync object available, they are used as events to synchronize. The parent and
// child have a pair of event mutexes each, which they own initially. To release the other waiting thread/process, the
// thread/process releases one of its mutexes, which the other thread/process would be waiting on. To wait, the thread/process
// waits on one of the other thread/process' mutexes. All the while, they ping-pong between the two mutexes. YieldToChild() and
// YieldToParent() below control the releasing, waiting, and ping-ponging, to help create a deterministic path through the
// parent and child tests while both are running concurrently.

bool AcquireChildRunningEvent(const char *testName, AutoCloseMutexHandle &childRunningEvent)
{
    char name[MaxPathSize];
    TestCreateMutex(childRunningEvent, BuildName(testName, name, GlobalPrefix, ChildRunningEventNamePrefix));
    TestAssert(WaitForSingleObject(childRunningEvent, FailTimeoutMilliseconds) == WAIT_OBJECT_0);
    return true;
}

bool InitializeParent(const char *testName, AutoCloseMutexHandle parentEvents[2], AutoCloseMutexHandle childEvents[2])
{
    // Create parent events
    char name[MaxPathSize];
    for (int i = 0; i < 2; ++i)
    {
        TestCreateMutex(
            parentEvents[i],
            BuildName(testName, name, GlobalPrefix, i == 0 ? ParentEventNamePrefix0 : ParentEventNamePrefix1),
            true);
        TestAssert(parentEvents[i] != nullptr);
        TestAssert(GetLastError() != ERROR_ALREADY_EXISTS);
    }

    // Wait for the child to create and acquire locks on its events so that the parent can wait on them
    TestAssert(WaitForMutexToBeCreated(testName, childEvents[0], ChildEventNamePrefix0));
    TestAssert(WaitForMutexToBeCreated(testName, childEvents[1], ChildEventNamePrefix1));
    return true;
}

bool UninitializeParent(const char *testName, AutoCloseMutexHandle parentEvents[2], bool releaseParentEvents = true)
{
    if (releaseParentEvents)
    {
        TestAssert(parentEvents[0].Release());
        TestAssert(parentEvents[1].Release());
    }

    // Wait for the child to finish its test. Child tests will release and close 'childEvents' before releasing
    // 'childRunningEvent', so after this wait, the parent process can freely start another child that will deterministically
    // recreate the 'childEvents', which the next parent test will wait on, upon its initialization.
    AutoCloseMutexHandle childRunningEvent;
    TestAssert(AcquireChildRunningEvent(testName, childRunningEvent));
    TestAssert(childRunningEvent.Release());
    return true;
}

bool InitializeChild(
    const char *testName,
    AutoCloseMutexHandle &childRunningEvent,
    AutoCloseMutexHandle parentEvents[2],
    AutoCloseMutexHandle childEvents[2])
{
    TestAssert(AcquireChildRunningEvent(testName, childRunningEvent));

    // Create child events
    char name[MaxPathSize];
    for (int i = 0; i < 2; ++i)
    {
        TestCreateMutex(
            childEvents[i],
            BuildName(testName, name, GlobalPrefix, i == 0 ? ChildEventNamePrefix0 : ChildEventNamePrefix1),
            true);
        TestAssert(childEvents[i] != nullptr);
        TestAssert(GetLastError() != ERROR_ALREADY_EXISTS);
    }

    // Wait for the parent to create and acquire locks on its events so that the child can wait on them
    TestAssert(WaitForMutexToBeCreated(testName, parentEvents[0], ParentEventNamePrefix0));
    TestAssert(WaitForMutexToBeCreated(testName, parentEvents[1], ParentEventNamePrefix1));

    // Parent/child tests start with the parent, so after initialization, wait for the parent to tell the child test to start
    TestAssert(WaitForSingleObject(parentEvents[0], FailTimeoutMilliseconds) == WAIT_OBJECT_0);
    TestAssert(parentEvents[0].Release());
    return true;
}

bool UninitializeChild(
    AutoCloseMutexHandle &childRunningEvent,
    AutoCloseMutexHandle parentEvents[2],
    AutoCloseMutexHandle childEvents[2])
{
    // Release and close 'parentEvents' and 'childEvents' before releasing 'childRunningEvent' to avoid races, see
    // UnitializeParent() for more info
    TestAssert(childEvents[0].Release());
    TestAssert(childEvents[1].Release());
    childEvents[0].Close();
    childEvents[1].Close();
    parentEvents[0].Close();
    parentEvents[1].Close();
    TestAssert(childRunningEvent.Release());
    return true;
}

bool YieldToChild(AutoCloseMutexHandle parentEvents[2], AutoCloseMutexHandle childEvents[2], int &ei)
{
    TestAssert(parentEvents[ei].Release());
    TestAssert(WaitForSingleObject(childEvents[ei], FailTimeoutMilliseconds) == WAIT_OBJECT_0);
    TestAssert(childEvents[ei].Release());
    TestAssert(WaitForSingleObject(parentEvents[ei], 0) == WAIT_OBJECT_0);
    ei = 1 - ei;
    return true;
}

bool YieldToParent(AutoCloseMutexHandle parentEvents[2], AutoCloseMutexHandle childEvents[2], int &ei)
{
    TestAssert(childEvents[ei].Release());
    ei = 1 - ei;
    TestAssert(WaitForSingleObject(parentEvents[ei], FailTimeoutMilliseconds) == WAIT_OBJECT_0);
    TestAssert(parentEvents[ei].Release());
    TestAssert(WaitForSingleObject(childEvents[1 - ei], 0) == WAIT_OBJECT_0);
    return true;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Tests

bool NameTests()
{
    const char *testName = "NameTests";

    AutoCloseMutexHandle m;
    char name[MaxPathSize];

    // Empty name
    TestCreateMutex(m, "");
    TestAssert(m != nullptr);

    // Normal name
    TestCreateMutex(m, BuildName(testName, name, NamePrefix));
    TestAssert(m != nullptr);
    TestAssert(AutoCloseMutexHandle(TestOpenMutex(BuildName(testName, name, NamePrefix))) != nullptr);
    TestCreateMutex(m, BuildName(testName, name, SessionPrefix, NamePrefix));
    TestAssert(m != nullptr);
    TestAssert(AutoCloseMutexHandle(TestOpenMutex(BuildName(testName, name, SessionPrefix, NamePrefix))) != nullptr);
    TestCreateMutex(m, BuildName(testName, name, GlobalPrefix, NamePrefix));
    TestAssert(m != nullptr);
    TestAssert(AutoCloseMutexHandle(TestOpenMutex(BuildName(testName, name, GlobalPrefix, NamePrefix))) != nullptr);

    // Name too long. The maximum allowed length depends on the file system, so we're not checking for that.
    {
        char name[257];
        memset(name, 'a', _countof(name) - 1);
        name[_countof(name) - 1] = '\0';
        TestCreateMutex(m, name);
        TestAssert(m == nullptr);
        TestAssert(GetLastError() == ERROR_FILENAME_EXCED_RANGE);
        TestAssert(AutoCloseMutexHandle(TestOpenMutex(name)) == nullptr);
        TestAssert(GetLastError() == ERROR_FILENAME_EXCED_RANGE);
    }

    // Invalid characters in name
    TestCreateMutex(m, BuildName(testName, name, InvalidNamePrefix0));
    TestAssert(m == nullptr);
    TestAssert(GetLastError() == ERROR_INVALID_NAME);
    TestAssert(AutoCloseMutexHandle(TestOpenMutex(BuildName(testName, name, InvalidNamePrefix0))) == nullptr);
    TestAssert(GetLastError() == ERROR_INVALID_NAME);
    TestCreateMutex(m, BuildName(testName, name, InvalidNamePrefix1));
    TestAssert(m == nullptr);
    TestAssert(GetLastError() == ERROR_INVALID_NAME);
    TestAssert(AutoCloseMutexHandle(TestOpenMutex(BuildName(testName, name, InvalidNamePrefix1))) == nullptr);
    TestAssert(GetLastError() == ERROR_INVALID_NAME);
    TestCreateMutex(m, BuildName(testName, name, SessionPrefix, InvalidNamePrefix0));
    TestAssert(m == nullptr);
    TestAssert(GetLastError() == ERROR_INVALID_NAME);
    TestAssert(AutoCloseMutexHandle(TestOpenMutex(BuildName(testName, name, SessionPrefix, InvalidNamePrefix0))) == nullptr);
    TestAssert(GetLastError() == ERROR_INVALID_NAME);
    TestCreateMutex(m, BuildName(testName, name, GlobalPrefix, InvalidNamePrefix1));
    TestAssert(m == nullptr);
    TestAssert(GetLastError() == ERROR_INVALID_NAME);
    TestAssert(AutoCloseMutexHandle(TestOpenMutex(BuildName(testName, name, GlobalPrefix, InvalidNamePrefix1))) == nullptr);
    TestAssert(GetLastError() == ERROR_INVALID_NAME);

    // Creating a second reference to the same named mutex yields an error indicating that it was opened, not created
    {
        TestCreateMutex(m, BuildName(testName, name, NamePrefix));
        TestAssert(m != nullptr);
        AutoCloseMutexHandle m2;
        TestCreateMutex(m2, BuildName(testName, name, NamePrefix));
        TestAssert(m2 != nullptr);
        TestAssert(GetLastError() == ERROR_ALREADY_EXISTS);
    }

    return true;
}

bool HeaderMismatchTests()
{
    const char *testName = "HeaderMismatchTests";

    AutoCloseMutexHandle m, m2;
    char name[MaxPathSize];
    int fd;

    // Create and hold onto a mutex during this test to create the shared memory directory
    TestCreateMutex(m2, BuildName(testName, name, GlobalPrefix, TempNamePrefix));
    TestAssert(m2 != nullptr);

    // Unknown shared memory type
    TestAssert(WriteHeaderInfo(BuildGlobalShmFilePath(testName, name, NamePrefix), -1, 1, &fd));
    TestCreateMutex(m, BuildName(testName, name, GlobalPrefix, NamePrefix));
    TestAssert(m == nullptr);
    TestAssert(GetLastError() == ERROR_INVALID_HANDLE);
    TestAssert(test_close(fd) == 0);
    TestAssert(test_unlink(BuildGlobalShmFilePath(testName, name, NamePrefix)) == 0);

    // Mismatched version
    TestAssert(WriteHeaderInfo(BuildGlobalShmFilePath(testName, name, NamePrefix), 0, -1, &fd));
    TestCreateMutex(m, BuildName(testName, name, GlobalPrefix, NamePrefix));
    TestAssert(m == nullptr);
    TestAssert(GetLastError() == ERROR_INVALID_HANDLE);
    TestAssert(test_close(fd) == 0);
    TestAssert(test_unlink(BuildGlobalShmFilePath(testName, name, NamePrefix)) == 0);

    return true;
}

bool MutualExclusionTests_Parent()
{
    const char *testName = "MutualExclusionTests";

    AutoCloseMutexHandle parentEvents[2], childEvents[2];
    TestAssert(InitializeParent(testName, parentEvents, childEvents));
    int ei = 0;
    char name[MaxPathSize];
    AutoCloseMutexHandle m;

    TestCreateMutex(m, BuildName(testName, name, GlobalPrefix, NamePrefix));
    TestAssert(m != nullptr);

    // Recursive locking with various timeouts
    TestAssert(WaitForSingleObject(m, 0) == WAIT_OBJECT_0);
    TestAssert(WaitForSingleObject(m, FailTimeoutMilliseconds) == WAIT_OBJECT_0);
    TestAssert(WaitForSingleObject(m, static_cast<DWORD>(-1)) == WAIT_OBJECT_0);
    TestAssert(m.Release());
    TestAssert(m.Release());
    TestAssert(m.Release());
    TestAssert(!m.Release()); // try to release the lock while nobody owns it, and verify recursive lock counting
    TestAssert(GetLastError() == ERROR_NOT_OWNER);

    TestAssert(YieldToChild(parentEvents, childEvents, ei)); // child takes the lock

    TestAssert(WaitForSingleObject(m, 0) == WAIT_TIMEOUT); // try to lock the mutex without waiting
    TestAssert(WaitForSingleObject(m, g_expectedTimeoutMilliseconds) == WAIT_TIMEOUT); // try to lock the mutex with a timeout
    TestAssert(!m.Release()); // try to release the lock while another thread owns it
    TestAssert(GetLastError() == ERROR_NOT_OWNER);

    TestAssert(YieldToChild(parentEvents, childEvents, ei)); // child releases the lock

    TestAssert(WaitForSingleObject(m, static_cast<DWORD>(-1)) == WAIT_OBJECT_0); // lock the mutex with no timeout and release
    TestAssert(m.Release());

    UninitializeParent(testName, parentEvents);
    return true;
}

DWORD MutualExclusionTests_Child(void *arg = nullptr)
{
    const char *testName = "MutualExclusionTests";

    AutoCloseMutexHandle childRunningEvent, parentEvents[2], childEvents[2];
    TestAssert(InitializeChild(testName, childRunningEvent, parentEvents, childEvents));
    int ei = 0;

    {
        char name[MaxPathSize];
        AutoCloseMutexHandle m;

        TestCreateMutex(m, BuildName(testName, name, GlobalPrefix, NamePrefix));
        TestAssert(m != nullptr);
        TestAssert(WaitForSingleObject(m, 0) == WAIT_OBJECT_0); // lock the mutex
        YieldToParent(parentEvents, childEvents, ei); // parent attempts to lock/release, and fails
        TestAssert(m.Release()); // release the lock
    }

    UninitializeChild(childRunningEvent, parentEvents, childEvents);
    return 0;
}

bool MutualExclusionTests()
{
    const char *testName = "MutualExclusionTests";

    {
        AutoCloseMutexHandle m;
        char name[MaxPathSize];

        // Releasing a lock that is not owned by any thread fails
        TestCreateMutex(m, BuildName(testName, name, NamePrefix));
        TestAssert(m != nullptr);
        TestAssert(!m.Release());
        TestAssert(GetLastError() == ERROR_NOT_OWNER);

        // Acquire a lock during upon creation, and release
        TestCreateMutex(m, BuildName(testName, name, NamePrefix), true);
        TestAssert(m != nullptr);
        TestAssert(m.Release());

        // Multi-waits including a named mutex are not supported
        AutoCloseMutexHandle m2;
        TestCreateMutex(m2, nullptr);
        TestAssert(m2 != nullptr);
        HANDLE waitHandles[] = {m2.GetHandle(), m.GetHandle()};
        TestAssert(
            WaitForMultipleObjects(
                _countof(waitHandles),
                waitHandles,
                false /* waitAll */,
                FailTimeoutMilliseconds) ==
            WAIT_FAILED);
        TestAssert(GetLastError() == ERROR_NOT_SUPPORTED);
        TestAssert(
            WaitForMultipleObjects(
                _countof(waitHandles),
                waitHandles,
                true /* waitAll */,
                FailTimeoutMilliseconds) ==
            WAIT_FAILED);
        TestAssert(GetLastError() == ERROR_NOT_SUPPORTED);
    }

    // When another thread or process owns the lock, this process should not be able to acquire a lock, and the converse
    TestAssert(StartThread(MutualExclusionTests_Child));
    TestAssert(MutualExclusionTests_Parent());
    TestAssert(StartProcess("MutualExclusionTests_Child"));
    TestAssert(MutualExclusionTests_Parent());

    return true;
}

bool LifetimeTests_Parent()
{
    const char *testName = "LifetimeTests";

    AutoCloseMutexHandle parentEvents[2], childEvents[2];
    TestAssert(InitializeParent(testName, parentEvents, childEvents));
    int ei = 0;
    char name[MaxPathSize];
    AutoCloseMutexHandle m;

    TestCreateMutex(m, BuildName(testName, name, GlobalPrefix, NamePrefix)); // create first reference to mutex
    TestAssert(m != nullptr);
    TestAssert(TestFileExists(BuildGlobalShmFilePath(testName, name, NamePrefix)));
    TestAssert(YieldToChild(parentEvents, childEvents, ei)); // child creates second reference to mutex using CreateMutex
    m.Close(); // close first reference
    TestAssert(TestFileExists(BuildGlobalShmFilePath(testName, name, NamePrefix)));
    TestAssert(YieldToChild(parentEvents, childEvents, ei)); // child closes second reference
    TestAssert(!TestFileExists(BuildGlobalShmFilePath(testName, name, NamePrefix)));

    TestCreateMutex(m, BuildName(testName, name, GlobalPrefix, NamePrefix)); // create first reference to mutex
    TestAssert(m != nullptr);
    TestAssert(TestFileExists(BuildGlobalShmFilePath(testName, name, NamePrefix)));
    TestAssert(YieldToChild(parentEvents, childEvents, ei)); // child creates second reference to mutex using OpenMutex
    m.Close(); // close first reference
    TestAssert(TestFileExists(BuildGlobalShmFilePath(testName, name, NamePrefix)));
    TestAssert(YieldToChild(parentEvents, childEvents, ei)); // child closes second reference
    TestAssert(!TestFileExists(BuildGlobalShmFilePath(testName, name, NamePrefix)));

    UninitializeParent(testName, parentEvents);
    return true;
}

DWORD LifetimeTests_Child(void *arg = nullptr)
{
    const char *testName = "LifetimeTests";

    AutoCloseMutexHandle childRunningEvent, parentEvents[2], childEvents[2];
    TestAssert(InitializeChild(testName, childRunningEvent, parentEvents, childEvents));
    int ei = 0;

    {
        char name[MaxPathSize];
        AutoCloseMutexHandle m;

        // ... parent creates first reference to mutex
        TestCreateMutex(m, BuildName(testName, name, GlobalPrefix, NamePrefix)); // create second reference to mutex using CreateMutex
        TestAssert(m != nullptr);
        TestAssert(YieldToParent(parentEvents, childEvents, ei)); // parent closes first reference
        m.Close(); // close second reference

        TestAssert(YieldToParent(parentEvents, childEvents, ei)); // parent verifies, and creates first reference to mutex again
        m = TestOpenMutex(BuildName(testName, name, GlobalPrefix, NamePrefix)); // create second reference to mutex using OpenMutex
        TestAssert(m != nullptr);
        TestAssert(YieldToParent(parentEvents, childEvents, ei)); // parent closes first reference
        m.Close(); // close second reference

        TestAssert(YieldToParent(parentEvents, childEvents, ei)); // parent verifies
    }

    UninitializeChild(childRunningEvent, parentEvents, childEvents);
    return 0;
}

bool LifetimeTests()
{
    const char *testName = "LifetimeTests";

    {
        AutoCloseMutexHandle m;
        char name[MaxPathSize];

        // Shm file should be created and deleted
        TestCreateMutex(m, BuildName(testName, name, GlobalPrefix, NamePrefix));
        TestAssert(m != nullptr);
        TestAssert(TestFileExists(BuildGlobalShmFilePath(testName, name, NamePrefix)));
        m.Close();
        TestAssert(!TestFileExists(BuildGlobalShmFilePath(testName, name, NamePrefix)));
    }

    // Shm file should not be deleted until last reference is released
    TestAssert(StartThread(LifetimeTests_Child));
    TestAssert(LifetimeTests_Parent());
    TestAssert(StartProcess("LifetimeTests_Child"));
    TestAssert(LifetimeTests_Parent());

    return true;
}

DWORD AbandonTests_Child_TryLock(void *arg = nullptr);

bool AbandonTests_Parent()
{
    const char *testName = "AbandonTests";

    char name[MaxPathSize];
    AutoCloseMutexHandle m;
    {
        AutoCloseMutexHandle parentEvents[2], childEvents[2];
        TestAssert(InitializeParent(testName, parentEvents, childEvents));
        int ei = 0;

        TestCreateMutex(m, BuildName(testName, name, GlobalPrefix, NamePrefix));
        TestAssert(m != nullptr);
        TestAssert(YieldToChild(parentEvents, childEvents, ei)); // child locks mutex
        TestAssert(parentEvents[0].Release());
        TestAssert(parentEvents[1].Release()); // child sleeps for short duration and abandons the mutex
        TestAssert(WaitForSingleObject(m, FailTimeoutMilliseconds) == WAIT_ABANDONED_0); // attempt to lock and see abandoned mutex

        UninitializeParent(testName, parentEvents, false /* releaseParentEvents */); // parent events are released above
    }

    // Verify that the mutex lock is owned by this thread, by starting a new thread and trying to lock it
    StartThread(AbandonTests_Child_TryLock);
    {
        AutoCloseMutexHandle parentEvents[2], childEvents[2];
        TestAssert(InitializeParent(testName, parentEvents, childEvents));
        int ei = 0;

        TestAssert(YieldToChild(parentEvents, childEvents, ei)); // child tries to lock mutex

        UninitializeParent(testName, parentEvents);
    }

    // Verify that the mutex lock is owned by this thread, by starting a new process and trying to lock it
    StartProcess("AbandonTests_Child_TryLock");
    AutoCloseMutexHandle parentEvents[2], childEvents[2];
    TestAssert(InitializeParent(testName, parentEvents, childEvents));
    int ei = 0;

    TestAssert(YieldToChild(parentEvents, childEvents, ei)); // child tries to lock mutex

    // Continue verification
    TestAssert(m.Release());
    TestAssert(WaitForSingleObject(m, FailTimeoutMilliseconds) == WAIT_OBJECT_0); // lock again to see it's not abandoned anymore
    TestAssert(m.Release());

    UninitializeParent(testName, parentEvents, false /* releaseParentEvents */); // parent events are released above

    // Since the child abandons the mutex, and a child process may not release the file lock on the shared memory file before
    // indicating completion to the parent, make sure to delete the shared memory file by repeatedly opening/closing the mutex
    // until the parent process becomes the last process to reference the mutex and closing it deletes the file.
    DWORD startTime = GetTickCount();
    while (true)
    {
        m.Close();
        if (!TestFileExists(BuildGlobalShmFilePath(testName, name, NamePrefix)))
        {
            break;
        }

        TestAssert(GetTickCount() - startTime < FailTimeoutMilliseconds);
        m = TestOpenMutex(BuildName(testName, name, GlobalPrefix, NamePrefix));
    }

    return true;
}

DWORD AbandonTests_Child_GracefulExit_Close(void *arg = nullptr)
{
    const char *testName = "AbandonTests";

    AutoCloseMutexHandle childRunningEvent, parentEvents[2], childEvents[2];
    TestAssert(InitializeChild(testName, childRunningEvent, parentEvents, childEvents));
    int ei = 0;

    {
        char name[MaxPathSize];
        AutoCloseMutexHandle m;

        // ... parent waits for child to lock mutex
        TestCreateMutex(m, BuildName(testName, name, GlobalPrefix, NamePrefix));
        TestAssert(m != nullptr);
        TestAssert(WaitForSingleObject(m, 0) == WAIT_OBJECT_0);
        TestAssert(YieldToParent(parentEvents, childEvents, ei)); // parent waits on mutex
        Sleep(g_expectedTimeoutMilliseconds); // wait for parent to wait on mutex
        m.Close(); // close mutex without releasing lock
    }

    UninitializeChild(childRunningEvent, parentEvents, childEvents);
    return 0;
}

DWORD AbandonTests_Child_GracefulExit_NoClose(void *arg = nullptr)
{
    const char *testName = "AbandonTests";

    // This test needs to run in a separate process because it does not close the mutex handle. Running it in a separate thread
    // causes the mutex object to retain a reference until the process terminates.
    TestAssert(test_getpid() != g_parentPid);

    AutoCloseMutexHandle childRunningEvent, parentEvents[2], childEvents[2];
    TestAssert(InitializeChild(testName, childRunningEvent, parentEvents, childEvents));
    int ei = 0;

    {
        char name[MaxPathSize];
        AutoCloseMutexHandle m;

        // ... parent waits for child to lock mutex
        TestCreateMutex(m, BuildName(testName, name, GlobalPrefix, NamePrefix));
        TestAssert(m != nullptr);
        TestAssert(WaitForSingleObject(m, 0) == WAIT_OBJECT_0);
        TestAssert(YieldToParent(parentEvents, childEvents, ei)); // parent waits on mutex
        Sleep(g_expectedTimeoutMilliseconds); // wait for parent to wait on mutex
        m.Abandon(); // don't close the mutex
    }

    UninitializeChild(childRunningEvent, parentEvents, childEvents);
    return 0;
}

DWORD AbandonTests_Child_AbruptExit(void *arg = nullptr)
{
    const char *testName = "AbandonTests";

    DWORD currentPid = test_getpid();
    TestAssert(currentPid != g_parentPid); // this test needs to run in a separate process

    {
        AutoCloseMutexHandle childRunningEvent, parentEvents[2], childEvents[2];
        TestAssert(InitializeChild(testName, childRunningEvent, parentEvents, childEvents));
        int ei = 0;

        {
            char name[MaxPathSize];
            AutoCloseMutexHandle m;

            // ... parent waits for child to lock mutex
            TestCreateMutex(m, BuildName(testName, name, GlobalPrefix, NamePrefix));
            TestAssert(m != nullptr);
            TestAssert(WaitForSingleObject(m, 0) == WAIT_OBJECT_0);
            TestAssert(YieldToParent(parentEvents, childEvents, ei)); // parent waits on mutex
            Sleep(g_expectedTimeoutMilliseconds); // wait for parent to wait on mutex
            m.Abandon(); // don't close the mutex
        }

        UninitializeChild(childRunningEvent, parentEvents, childEvents);
    }

    TestAssert(test_kill(currentPid) == 0); // abandon the mutex abruptly
    return 0;
}

DWORD AbandonTests_Child_TryLock(void *arg)
{
    const char *testName = "AbandonTests";

    AutoCloseMutexHandle childRunningEvent, parentEvents[2], childEvents[2];
    TestAssert(InitializeChild(testName, childRunningEvent, parentEvents, childEvents));
    int ei = 0;

    {
        char name[MaxPathSize];
        AutoCloseMutexHandle m;

        // ... parent waits for child to lock mutex
        TestCreateMutex(m, BuildName(testName, name, GlobalPrefix, NamePrefix));
        TestAssert(m != nullptr);
        TestAssert(WaitForSingleObject(m, 0) == WAIT_TIMEOUT); // try to lock the mutex while the parent holds the lock
        TestAssert(WaitForSingleObject(m, g_expectedTimeoutMilliseconds) == WAIT_TIMEOUT);
    }

    UninitializeChild(childRunningEvent, parentEvents, childEvents);
    return 0;
}

bool AbandonTests()
{
    const char *testName = "AbandonTests";

    // Abandon by graceful exit where the lock owner closes the mutex before releasing it, unblocks a waiter
    TestAssert(StartThread(AbandonTests_Child_GracefulExit_Close));
    TestAssert(AbandonTests_Parent());
    TestAssert(StartProcess("AbandonTests_Child_GracefulExit_Close"));
    TestAssert(AbandonTests_Parent());

    // Abandon by graceful exit without closing the mutex unblocks a waiter
    TestAssert(StartProcess("AbandonTests_Child_GracefulExit_NoClose"));
    TestAssert(AbandonTests_Parent());

    // Abandon by abrupt exit unblocks a waiter
    TestAssert(StartProcess("AbandonTests_Child_AbruptExit"));
    TestAssert(AbandonTests_Parent());

    return true;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Test harness

bool (*const (TestList[]))() =
{
    NameTests,
    HeaderMismatchTests,
    MutualExclusionTests,
    LifetimeTests,
    AbandonTests
};

bool RunTests()
{
    bool allPassed = true;
    for (SIZE_T i = 0; i < _countof(TestList); ++i)
    {
        if (!TestList[i]())
        {
            allPassed = false;
        }
    }
    return allPassed;
}

DWORD g_stressDurationMilliseconds = 0;
LONG g_stressTestCounts[_countof(TestList)] = {0};
LONG g_stressResult = true;

DWORD StressTest(void *arg)
{
    // Run the specified test continuously for the stress duration
    SIZE_T testIndex = reinterpret_cast<SIZE_T>(arg);
    DWORD startTime = GetTickCount();
    do
    {
        ++g_stressTestCounts[testIndex];
        if (!TestList[testIndex]())
        {
            InterlockedExchange(&g_stressResult, false);
            break;
        }
    } while (
        InterlockedCompareExchange(&g_stressResult, false, false) == true &&
        GetTickCount() - startTime < g_stressDurationMilliseconds);
    return 0;
}

bool StressTests(DWORD durationMinutes)
{
    g_isStress = true;
    g_expectedTimeoutMilliseconds = 1;
    g_stressDurationMilliseconds = durationMinutes * (60 * 1000);

    // Start a thread for each test
    HANDLE threadHandles[_countof(TestList)];
    for (SIZE_T i = 0; i < _countof(threadHandles); ++i)
    {
        TestAssert(StartThread(StressTest, reinterpret_cast<void *>(i), &threadHandles[i]));
    }

    while (true)
    {
        DWORD waitResult =
            WaitForMultipleObjects(_countof(threadHandles), threadHandles, true /* bWaitAll */, 10 * 1000 /* dwMilliseconds */);
        TestAssert(waitResult == WAIT_OBJECT_0 || waitResult == WAIT_TIMEOUT);
        if (waitResult == WAIT_OBJECT_0)
        {
            break;
        }

        Trace("'paltest_namedmutex_test1' stress test counts: ");
        for (SIZE_T i = 0; i < _countof(g_stressTestCounts); ++i)
        {
            if (i != 0)
            {
                Trace(", ");
            }
            Trace("%u", g_stressTestCounts[i]);
        }
        Trace("\n");
        fflush(stdout);
    }

    for (SIZE_T i = 0; i < _countof(threadHandles); ++i)
    {
        CloseHandle(threadHandles[i]);
    }
    return static_cast<bool>(g_stressResult);
}

int __cdecl main(int argc, char **argv)
{
    if (argc < 1 || argc > 4)
    {
        return FAIL;
    }

    if (PAL_Initialize(argc, argv) != 0)
    {
        return FAIL;
    }

    test_strcpy(g_processPath, argv[0]);

    if (argc == 1)
    {
        // Unit test arguments: <processPath>

        g_parentPid = test_getpid();
        int result = RunTests() ? PASS : FAIL;
        ExitProcess(result);
        return result;
    }

    if (test_strcmp(argv[1], "stress") == 0)
    {
        // Stress test arguments: <processPath> stress [durationMinutes]

        DWORD durationMinutes = 1;
        if (argc >= 3 && test_sscanf(argv[2], "%u", &durationMinutes) != 1)
        {
            ExitProcess(FAIL);
            return FAIL;
        }

        g_parentPid = test_getpid();
        int result = StressTests(durationMinutes) ? PASS : FAIL;
        ExitProcess(result);
        return result;
    }

    // Child test process arguments: <processPath> <parentPid> <testFunctionName> [stress]

    g_isParent = false;

    // Get parent process' ID from argument
    if (test_sscanf(argv[1], "%u", &g_parentPid) != 1)
    {
        ExitProcess(FAIL);
        return FAIL;
    }

    if (argc >= 4 && test_strcmp(argv[3], "stress") == 0)
    {
        g_isStress = true;
    }

    if (test_strcmp(argv[2], "MutualExclusionTests_Child") == 0)
    {
        MutualExclusionTests_Child();
    }
    else if (test_strcmp(argv[2], "LifetimeTests_Child") == 0)
    {
        LifetimeTests_Child();
    }
    else if (test_strcmp(argv[2], "AbandonTests_Child_GracefulExit_Close") == 0)
    {
        AbandonTests_Child_GracefulExit_Close();
    }
    else if (test_strcmp(argv[2], "AbandonTests_Child_GracefulExit_NoClose") == 0)
    {
        AbandonTests_Child_GracefulExit_NoClose();
    }
    else if (test_strcmp(argv[2], "AbandonTests_Child_AbruptExit") == 0)
    {
        AbandonTests_Child_AbruptExit();
    }
    else if (test_strcmp(argv[2], "AbandonTests_Child_TryLock") == 0)
    {
        AbandonTests_Child_TryLock();
    }
    ExitProcess(PASS);
    return PASS;
}