|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Reflection; |
| 6 | +using System.Reflection.Emit; |
| 7 | +using System.Runtime.CompilerServices; |
| 8 | +using System.Runtime.InteropServices; |
| 9 | +using System.Threading; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +namespace CollectibleThreadStaticShutdownRace |
| 13 | +{ |
| 14 | + public class CollectibleThreadStaticShutdownRace |
| 15 | + { |
| 16 | + Action? UseTLSStaticFromLoaderAllocator = null; |
| 17 | + GCHandle IsLoaderAllocatorLive; |
| 18 | + static ulong s_collectibleIndex; |
| 19 | + |
| 20 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 21 | + void CallUseTLSStaticFromLoaderAllocator() |
| 22 | + { |
| 23 | + UseTLSStaticFromLoaderAllocator!(); |
| 24 | + UseTLSStaticFromLoaderAllocator = null; |
| 25 | + } |
| 26 | + |
| 27 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 28 | + bool CheckLALive() |
| 29 | + { |
| 30 | + return IsLoaderAllocatorLive.Target != null; |
| 31 | + } |
| 32 | + |
| 33 | + |
| 34 | + void ThreadThatWaitsForLoaderAllocatorToDisappear() |
| 35 | + { |
| 36 | + CallUseTLSStaticFromLoaderAllocator(); |
| 37 | + while (CheckLALive()) |
| 38 | + { |
| 39 | + GC.Collect(2); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + void CreateLoaderAllocatorWithTLS() |
| 44 | + { |
| 45 | + ulong collectibleIndex = s_collectibleIndex++; |
| 46 | + |
| 47 | + var ab = |
| 48 | + AssemblyBuilder.DefineDynamicAssembly( |
| 49 | + new AssemblyName($"CollectibleDerivedAssembly{collectibleIndex}"), |
| 50 | + AssemblyBuilderAccess.RunAndCollect); |
| 51 | + var mob = ab.DefineDynamicModule($"CollectibleDerivedModule{collectibleIndex}"); |
| 52 | + var tb = |
| 53 | + mob.DefineType( |
| 54 | + $"CollectibleDerived{collectibleIndex}", |
| 55 | + TypeAttributes.Class | TypeAttributes.Public, |
| 56 | + typeof(object)); |
| 57 | + |
| 58 | + { |
| 59 | + var fb = |
| 60 | + tb.DefineField( |
| 61 | + "Field", typeof(int), FieldAttributes.Static); |
| 62 | + fb.SetCustomAttribute(typeof(ThreadStaticAttribute).GetConstructors()[0], new byte[0]); |
| 63 | + |
| 64 | + var mb = |
| 65 | + tb.DefineMethod( |
| 66 | + "Method", |
| 67 | + MethodAttributes.Public | MethodAttributes.Static); |
| 68 | + var ilg = mb.GetILGenerator(); |
| 69 | + ilg.Emit(OpCodes.Ldc_I4_1); |
| 70 | + ilg.Emit(OpCodes.Stsfld, fb); |
| 71 | + ilg.Emit(OpCodes.Ret); |
| 72 | + } |
| 73 | + |
| 74 | + Type createdType = tb.CreateType(); |
| 75 | + UseTLSStaticFromLoaderAllocator = (Action)createdType.GetMethods()[0].CreateDelegate(typeof(Action)); |
| 76 | + IsLoaderAllocatorLive = GCHandle.Alloc(createdType, GCHandleType.WeakTrackResurrection); |
| 77 | + } |
| 78 | + |
| 79 | + void ForceCollectibleTLSStaticToGoThroughThreadTermination() |
| 80 | + { |
| 81 | + int iteration = 0; |
| 82 | + for (int i = 0; i < 10; i++) |
| 83 | + { |
| 84 | + Console.WriteLine($"Iteration {iteration++}"); |
| 85 | + var createLAThread = new Thread(CreateLoaderAllocatorWithTLS); |
| 86 | + createLAThread.Start(); |
| 87 | + createLAThread.Join(); |
| 88 | + |
| 89 | + var crashThread = new Thread(ThreadThatWaitsForLoaderAllocatorToDisappear); |
| 90 | + crashThread.Start(); |
| 91 | + crashThread.Join(); |
| 92 | + } |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + [Fact] |
| 97 | + public static void TestEntryPoint() |
| 98 | + { |
| 99 | + new CollectibleThreadStaticShutdownRace().ForceCollectibleTLSStaticToGoThroughThreadTermination(); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
0 commit comments