Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ClassConstructorRunner.cs « CompilerServices « Runtime « System « src « System.Private.CoreLib « src - github.com/mono/corert.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a82e434b8f6dd034900713a308ddf52271488946 (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
// 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.

using System;
using System.Threading;
using System.Diagnostics;
using System.Collections.Generic;
using System.Runtime.InteropServices;

using Internal.Runtime;
using Internal.Runtime.CompilerHelpers;

namespace System.Runtime.CompilerServices
{
    internal static partial class ClassConstructorRunner
    {
        //==============================================================================================================
        // Ensures the class constructor for the given type has run. 
        //
        // Called by the runtime when it finds a class whose static class constructor has probably not run
        // (probably because it checks in the initialized flag without thread synchronization).
        //
        // The context structure passed by reference lives in the image of one of the application's modules.
        // The contents are thus fixed (do not require pinning) and the address can be used as a unique
        // identifier for the context.
        //
        // This guarantee is violated in one specific case: where a class constructor cycle would cause a deadlock. If 
        // so, per ECMA specs, this method returns without guaranteeing that the .cctor has run.
        //
        // No attempt is made to detect or break deadlocks due to other synchronization mechanisms.
        //==============================================================================================================
#if PROJECTN
        [RuntimeExport("CheckStaticClassConstruction")]
        public static unsafe void* CheckStaticClassConstruction(void* returnValue, StaticClassConstructionContext* pContext)
        {
            EnsureClassConstructorRun(pContext);
            return returnValue;
        }
#else
        private static unsafe object CheckStaticClassConstructionReturnGCStaticBase(StaticClassConstructionContext* context, object gcStaticBase)
        {
            EnsureClassConstructorRun(context);
            return gcStaticBase;
        }

        private static unsafe IntPtr CheckStaticClassConstructionReturnNonGCStaticBase(StaticClassConstructionContext* context, IntPtr nonGcStaticBase)
        {
            EnsureClassConstructorRun(context);
            return nonGcStaticBase;
        }

        private unsafe static object CheckStaticClassConstructionReturnThreadStaticBase(TypeManagerSlot* pModuleData, int typeTlsIndex, StaticClassConstructionContext* context)
        {
            object threadStaticBase = ThreadStatics.GetThreadStaticBaseForType(pModuleData, typeTlsIndex);
            EnsureClassConstructorRun(context);
            return threadStaticBase;
        }
#endif

        public static unsafe void EnsureClassConstructorRun(StaticClassConstructionContext* pContext)
        {
            IntPtr pfnCctor = pContext->cctorMethodAddress;
            NoisyLog("EnsureClassConstructorRun, cctor={0}, thread={1}", pfnCctor, CurrentManagedThreadId);

            // If we were called from MRT, this check is redundant but harmless. This is in case someone within classlib
            // (cough, Reflection) needs to call this explicitly.
            if (pContext->initialized == 1)
            {
                NoisyLog("Cctor already run, cctor={0}, thread={1}", pfnCctor, CurrentManagedThreadId);
                return;
            }

            CctorHandle cctor = Cctor.GetCctor(pContext);
            Cctor[] cctors = cctor.Array;
            int cctorIndex = cctor.Index;
            try
            {
                Lock cctorLock = cctors[cctorIndex].Lock;
                if (DeadlockAwareAcquire(cctor, pfnCctor))
                {
                    int currentManagedThreadId = CurrentManagedThreadId;
                    try
                    {
                        NoisyLog("Acquired cctor lock, cctor={0}, thread={1}", pfnCctor, currentManagedThreadId);

                        cctors[cctorIndex].HoldingThread = currentManagedThreadId;
                        if (pContext->initialized == 0)  // Check again in case some thread raced us while we were acquiring the lock.
                        {
                            TypeInitializationException priorException = cctors[cctorIndex].Exception;
                            if (priorException != null)
                                throw priorException;
                            try
                            {
                                NoisyLog("Calling cctor, cctor={0}, thread={1}", pfnCctor, currentManagedThreadId);

                                Call(pfnCctor);

                                // Insert a memory barrier here to order any writes executed as part of static class
                                // construction above with respect to the initialized flag update we're about to make
                                // below. This is important since the fast path for checking the cctor uses a normal read
                                // and doesn't come here so without the barrier it could observe initialized == 1 but
                                // still see uninitialized static fields on the class.
                                Interlocked.MemoryBarrier();

                                NoisyLog("Set type inited, cctor={0}, thread={1}", pfnCctor, currentManagedThreadId);

                                pContext->initialized = 1;
                            }
                            catch (Exception e)
                            {
                                TypeInitializationException wrappedException = new TypeInitializationException(null, SR.TypeInitialization_Type_NoTypeAvailable, e);
                                cctors[cctorIndex].Exception = wrappedException;
                                throw wrappedException;
                            }
                        }
                    }
                    finally
                    {
                        cctors[cctorIndex].HoldingThread = ManagedThreadIdNone;
                        NoisyLog("Releasing cctor lock, cctor={0}, thread={1}", pfnCctor, currentManagedThreadId);

                        cctorLock.Release();
                    }
                }
                else
                {
                    // Cctor cycle resulted in a deadlock. We will break the guarantee and return without running the 
                    // .cctor.
                }
            }
            finally
            {
                Cctor.Release(cctor);
            }
            NoisyLog("EnsureClassConstructorRun complete, cctor={0}, thread={1}", pfnCctor, CurrentManagedThreadId);
        }

        //=========================================================================================================
        // Return value:
        //   true   - lock acquired. 
        //   false  - deadlock detected. Lock not acquired.
        //=========================================================================================================
        private static bool DeadlockAwareAcquire(CctorHandle cctor, IntPtr pfnCctor)
        {
            const int WaitIntervalSeedInMS = 1;      // seed with 1ms and double every time through the loop
            const int WaitIntervalLimitInMS = WaitIntervalSeedInMS << 7; // limit of 128ms

            int waitIntervalInMS = WaitIntervalSeedInMS;

            int cctorIndex = cctor.Index;
            Cctor[] cctors = cctor.Array;
            Lock lck = cctors[cctorIndex].Lock;
            if (lck.IsAcquired)
                return false;     // Thread recursively triggered the same cctor.

            if (lck.TryAcquire(waitIntervalInMS))
                return true;

            // We couldn't acquire the lock. See if this .cctor is involved in a cross-thread deadlock.  If so, break 
            // the deadlock by breaking the guarantee - we'll skip running the .cctor and let the caller take his chances.
            int currentManagedThreadId = CurrentManagedThreadId;
            int unmarkCookie = -1;
            try
            {
                // We'll spin in a forever-loop of checking for a deadlock state, then waiting a short time, then 
                // checking for a deadlock state again, and so on. This is because the BlockedRecord info has a built-in 
                // lag time - threads don't report themselves as blocking until they've been blocked for a non-trivial 
                // amount of time. 
                //
                // If the threads are deadlocked for any reason other a class constructor cycling, this loop will never 
                // terminate - this is by design. If the user code inside the class constructors were to 
                // deadlock themselves, then that's a bug in user code.
                for (;;)
                {
                    using (LockHolder.Hold(s_cctorGlobalLock))
                    {
                        // Ask the guy who holds the cctor lock we're trying to acquire who he's waiting for. Keep 
                        // walking down that chain until we either discover a cycle or reach a non-blocking state. Note 
                        // that reaching a non-blocking state is not proof that we've avoided a deadlock due to the 
                        // BlockingRecord reporting lag.
                        CctorHandle cctorWalk = cctor;
                        int chainStepCount = 0;
                        for (; chainStepCount < Cctor.Count; chainStepCount++)
                        {
                            int cctorWalkIndex = cctorWalk.Index;
                            Cctor[] cctorWalkArray = cctorWalk.Array;

                            int holdingThread = cctorWalkArray[cctorWalkIndex].HoldingThread;
                            if (holdingThread == currentManagedThreadId)
                            {
                                // Deadlock detected.  We will break the guarantee and return without running the .cctor.
                                DebugLog("A class constructor was skipped due to class constructor cycle. cctor={0}, thread={1}",
                                    pfnCctor, currentManagedThreadId);

                                // We are maintaining an invariant that the BlockingRecords never show a cycle because,
                                // before we add a record, we first check for a cycle.  As a result, once we've said 
                                // we're waiting, we are committed to waiting and will not need to skip running this 
                                // .cctor.
                                Debug.Assert(unmarkCookie == -1);
                                return false;
                            }

                            if (holdingThread == ManagedThreadIdNone)
                            {
                                // No one appears to be holding this cctor lock. Give the current thread some more time 
                                // to acquire the lock. 
                                break;
                            }

                            cctorWalk = BlockingRecord.GetCctorThatThreadIsBlockedOn(holdingThread);
                            if (cctorWalk.Array == null)
                            {
                                // The final thread in the chain appears to be blocked on nothing. Give the current 
                                // thread some more time to acquire the lock.
                                break;
                            }
                        }

                        // We don't allow cycles in the BlockingRecords, so we must always enumerate at most each entry,
                        // but never more.
                        Debug.Assert(chainStepCount < Cctor.Count);

                        // We have not discovered a deadlock, so let's register the fact that we're waiting on another 
                        // thread and continue to wait.  It is important that we only signal that we are blocked after 
                        // we check for a deadlock because, otherwise, we give all threads involved in the deadlock the 
                        // opportunity to break it themselves and that leads to "ping-ponging" between the cctors
                        // involved in the cycle, allowing intermediate cctor results to be observed.
                        //
                        // The invariant here is that we never 'publish' a BlockingRecord that forms a cycle.  So it is
                        // important that the look-for-cycle-and-then-publish-wait-status operation be atomic with 
                        // respect to other updates to the BlockingRecords.
                        if (unmarkCookie == -1)
                        {
                            NoisyLog("Mark thread blocked, cctor={0}, thread={1}", pfnCctor, currentManagedThreadId);

                            unmarkCookie = BlockingRecord.MarkThreadAsBlocked(currentManagedThreadId, cctor);
                        }
                    } // _cctorGlobalLock scope

                    if (waitIntervalInMS < WaitIntervalLimitInMS)
                        waitIntervalInMS *= 2;

                    // We didn't find a cycle yet, try to take the lock again.
                    if (lck.TryAcquire(waitIntervalInMS))
                        return true;
                } // infinite loop
            }
            finally
            {
                if (unmarkCookie != -1)
                {
                    NoisyLog("Unmark thread blocked, cctor={0}, thread={1}", pfnCctor, currentManagedThreadId);
                    BlockingRecord.UnmarkThreadAsBlocked(unmarkCookie);
                }
            }
        }

        //==============================================================================================================
        // These structs are allocated on demand whenever the runtime tries to run a class constructor. Once the
        // the class constructor has been successfully initialized, we reclaim this structure. The structure is long-
        // lived only if the class constructor threw an exception.
        //==============================================================================================================
        private unsafe struct Cctor
        {
            public Lock Lock;
            public TypeInitializationException Exception;
            public int HoldingThread;
            private int _refCount;
            private StaticClassConstructionContext* _pContext;

            //==========================================================================================================
            // Gets the Cctor entry associated with a specific class constructor context (creating it if necessary.)
            //==========================================================================================================
            public static CctorHandle GetCctor(StaticClassConstructionContext* pContext)
            {
#if DEBUG
                const int Grow = 2;
#else
                const int Grow = 10;
#endif

                // WASMTODO: Remove this when the Initialize method gets called by the runtime startup
#if WASM
                if (s_cctorGlobalLock == null)
                {
                    Interlocked.CompareExchange(ref s_cctorGlobalLock, new Lock(), null);
                }
                if (s_cctorArrays == null)
                {
                    Interlocked.CompareExchange(ref s_cctorArrays, new Cctor[10][], null);
                }
#endif // WASM

                using (LockHolder.Hold(s_cctorGlobalLock))
                {
                    Cctor[] resultArray = null;
                    int resultIndex = -1;

                    if (s_count != 0)
                    {
                        // Search for the cctor context in our existing arrays
                        for (int cctorIndex = 0; cctorIndex < s_cctorArraysCount; ++cctorIndex)
                        {
                            Cctor[] segment = s_cctorArrays[cctorIndex];
                            for (int i = 0; i < segment.Length; i++)
                            {
                                if (segment[i]._pContext == pContext)
                                {
                                    resultArray = segment;
                                    resultIndex = i;
                                    break;
                                }
                            }
                            if (resultArray != null)
                                break;
                        }
                    }
                    if (resultArray == null)
                    {
                        // look for an empty entry in an existing array
                        for (int cctorIndex = 0; cctorIndex < s_cctorArraysCount; ++cctorIndex)
                        {
                            Cctor[] segment = s_cctorArrays[cctorIndex];
                            for (int i = 0; i < segment.Length; i++)
                            {
                                if (segment[i]._pContext == default(StaticClassConstructionContext*))
                                {
                                    resultArray = segment;
                                    resultIndex = i;
                                    break;
                                }
                            }
                            if (resultArray != null)
                                break;
                        }
                        if (resultArray == null)
                        {
                            // allocate a new array
                            resultArray = new Cctor[Grow];
                            if (s_cctorArraysCount == s_cctorArrays.Length)
                            {
                                // grow the container
                                Array.Resize(ref s_cctorArrays, (s_cctorArrays.Length * 2) + 1);
                            }
                            // store the array in the container, this cctor gets index 0
                            s_cctorArrays[s_cctorArraysCount] = resultArray;
                            s_cctorArraysCount++;
                            resultIndex = 0;
                        }

                        Debug.Assert(resultArray[resultIndex]._pContext == default(StaticClassConstructionContext*));
                        resultArray[resultIndex]._pContext = pContext;
                        resultArray[resultIndex].Lock = new Lock();
                        s_count++;
                    }

                    Interlocked.Increment(ref resultArray[resultIndex]._refCount);
                    return new CctorHandle(resultArray, resultIndex);
                }
            }

            public static int Count
            {
                get
                {
                    Debug.Assert(s_cctorGlobalLock.IsAcquired);
                    return s_count;
                }
            }

            public static void Release(CctorHandle cctor)
            {
                using (LockHolder.Hold(s_cctorGlobalLock))
                {
                    Cctor[] cctors = cctor.Array;
                    int cctorIndex = cctor.Index;
                    if (0 == Interlocked.Decrement(ref cctors[cctorIndex]._refCount))
                    {
                        if (cctors[cctorIndex].Exception == null)
                        {
                            cctors[cctorIndex] = new Cctor();
                            s_count--;
                        }
                    }
                }
            }
        }

        private struct CctorHandle
        {
            public CctorHandle(Cctor[] array, int index)
            {
                _array = array;
                _index = index;
            }

            public Cctor[] Array { get { return _array; } }
            public int Index { get { return _index; } }

            private Cctor[] _array;
            private int _index;
        }

        //==============================================================================================================
        // Keeps track of threads that are blocked on a cctor lock (alas, we don't have ThreadLocals here in 
        // System.Private.CoreLib so we have to use a side table.)
        //
        // This is used for cross-thread deadlock detection. 
        //
        // - Data is only entered here if a thread has been blocked past a certain timeout (otherwise, it's certainly 
        //   not participating of a deadlock.)
        // - Reads and writes to _blockingRecord are guarded by _cctorGlobalLock.
        // - BlockingRecords for individual threads are created on demand. Since this is a rare event, we won't attempt 
        //   to recycle them directly (however,
        //   ManagedThreadId's are themselves recycled pretty quickly - and threads that inherit the managed id also 
        //   inherit the BlockingRecord.)
        //==============================================================================================================
        private struct BlockingRecord
        {
            public int ManagedThreadId;     // ManagedThreadId of the blocked thread
            public CctorHandle BlockedOn;

            public static int MarkThreadAsBlocked(int managedThreadId, CctorHandle blockedOn)
            {
#if DEBUG
                const int Grow = 2;
#else
                const int Grow = 10;
#endif
                using (LockHolder.Hold(s_cctorGlobalLock))
                {
                    if (s_blockingRecords == null)
                        s_blockingRecords = new BlockingRecord[Grow];
                    int found;
                    for (found = 0; found < s_nextBlockingRecordIndex; found++)
                    {
                        if (s_blockingRecords[found].ManagedThreadId == managedThreadId)
                            break;
                    }
                    if (found == s_nextBlockingRecordIndex)
                    {
                        if (s_nextBlockingRecordIndex == s_blockingRecords.Length)
                        {
                            BlockingRecord[] newBlockingRecords = new BlockingRecord[s_blockingRecords.Length + Grow];
                            for (int i = 0; i < s_blockingRecords.Length; i++)
                            {
                                newBlockingRecords[i] = s_blockingRecords[i];
                            }
                            s_blockingRecords = newBlockingRecords;
                        }
                        s_blockingRecords[s_nextBlockingRecordIndex].ManagedThreadId = managedThreadId;
                        s_nextBlockingRecordIndex++;
                    }
                    s_blockingRecords[found].BlockedOn = blockedOn;
                    return found;
                }
            }

            public static void UnmarkThreadAsBlocked(int blockRecordIndex)
            {
                // This method must never throw
                s_cctorGlobalLock.Acquire();
                s_blockingRecords[blockRecordIndex].BlockedOn = new CctorHandle(null, 0);
                s_cctorGlobalLock.Release();
            }

            public static CctorHandle GetCctorThatThreadIsBlockedOn(int managedThreadId)
            {
                Debug.Assert(s_cctorGlobalLock.IsAcquired);
                for (int i = 0; i < s_nextBlockingRecordIndex; i++)
                {
                    if (s_blockingRecords[i].ManagedThreadId == managedThreadId)
                        return s_blockingRecords[i].BlockedOn;
                }
                return new CctorHandle(null, 0);
            }

            private static BlockingRecord[] s_blockingRecords;
            private static int s_nextBlockingRecordIndex;
        }

        private static Lock s_cctorGlobalLock;

        // These three  statics are used by ClassConstructorRunner.Cctor but moved out to avoid an unnecessary 
        // extra class constructor call.
        //
        // Because Cctor's are mutable structs, we have to give our callers raw references to the underlying arrays 
        // for this collection to be usable.  This also means once we place a Cctor in an array, we can't grow or 
        // reallocate the array.
        private static Cctor[][] s_cctorArrays;
        private static int s_cctorArraysCount;
        private static int s_count;

        // Eager construction called from LibraryInitialize Cctor.GetCctor uses _cctorGlobalLock.
        internal static void Initialize()
        {
            s_cctorArrays = new Cctor[10][];
            s_cctorGlobalLock = new Lock();
        }

        [Conditional("ENABLE_NOISY_CCTOR_LOG")]
        private static void NoisyLog(string format, IntPtr cctorMethod, int threadId)
        {
            // We cannot utilize any of the typical number formatting code because it triggers globalization code to run 
            // and this cctor code is layered below globalization.
#if DEBUG
            Debug.WriteLine(format, ToHexString(cctorMethod), ToHexString(threadId));
#endif // DEBUG
        }

        [Conditional("DEBUG")]
        private static void DebugLog(string format, IntPtr cctorMethod, int threadId)
        {
            // We cannot utilize any of the typical number formatting code because it triggers globalization code to run 
            // and this cctor code is layered below globalization.
#if DEBUG
            Debug.WriteLine(format, ToHexString(cctorMethod), ToHexString(threadId));
#endif
        }

        // We cannot utilize any of the typical number formatting code because it triggers globalization code to run 
        // and this cctor code is layered below globalization.
#if DEBUG
        private static string ToHexString(int num)
        {
            return ToHexStringUnsignedLong((ulong)num, false, 8);
        }
        private static string ToHexString(IntPtr num)
        {
            return ToHexStringUnsignedLong((ulong)num, false, 16);
        }
        private static char GetHexChar(uint u)
        {
            if (u < 10)
                return unchecked((char)('0' + u));

            return unchecked((char)('a' + (u - 10)));
        }
        public static unsafe string ToHexStringUnsignedLong(ulong u, bool zeroPrepad, int numChars)
        {
            char[] chars = new char[numChars];

            int i = numChars - 1;

            for (; i >= 0; i--)
            {
                chars[i] = GetHexChar((uint)(u % 16));
                u = u / 16;

                if ((i == 0) || (!zeroPrepad && (u == 0)))
                    break;
            }

            string str;
            fixed (char* p = &chars[i])
            {
                str = new string(p, 0, numChars - i);
            }
            return str;
        }
#endif
    }
}