< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Runtime.Fx
Assembly: CoreWCF.NetNamedPipe
File(s): /home/runner/work/CoreWCF/CoreWCF/src/Common/src/CoreWCF/Runtime/Fx.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 116
Coverable lines: 116
Total lines: 436
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 40
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/Common/src/CoreWCF/Runtime/Fx.cs

#LineLine coverage
 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
 4using System;
 5using System.Collections.ObjectModel;
 6using System.Diagnostics;
 7using System.Diagnostics.Contracts;
 8using System.Reflection;
 9using System.Runtime.CompilerServices;
 10using System.Runtime.Serialization;
 11using System.Threading;
 12using CoreWCF.Runtime.Diagnostics;
 13using System.Runtime.Versioning;
 14
 15namespace CoreWCF.Runtime
 16{
 17    internal static class Fx
 18    {
 19        private const string defaultEventSource = "Microsoft.Runtime";
 20        private static ExceptionTrace s_exceptionTrace;
 21        private static EtwDiagnosticTrace s_diagnosticTrace;
 22
 23        public static ExceptionTrace Exception
 24        {
 25            get
 26            {
 027                if (s_exceptionTrace == null)
 28                {
 29                    // don't need a lock here since a true singleton is not required
 030                    s_exceptionTrace = new ExceptionTrace(defaultEventSource, Trace);
 31                }
 32
 033                return s_exceptionTrace;
 34            }
 35        }
 36
 37        public static EtwDiagnosticTrace Trace
 38        {
 39            get
 40            {
 041                if (s_diagnosticTrace == null)
 42                {
 043                    s_diagnosticTrace = InitializeTracing();
 44                }
 45
 046                return s_diagnosticTrace;
 47            }
 48        }
 49
 50        private static EtwDiagnosticTrace InitializeTracing()
 51        {
 052            EtwDiagnosticTrace trace = new EtwDiagnosticTrace(defaultEventSource, EtwDiagnosticTrace.DefaultEtwProviderI
 53
 54            //if (null != trace.EtwProvider)
 55            //{
 56            //    trace.RefreshState += delegate ()
 57            //    {
 58            //        Fx.UpdateLevel();
 59            //    };
 60            //}
 61            //Fx.UpdateLevel(trace);
 062            return trace;
 63        }
 64
 065        public static ExceptionHandler AsynchronousThreadExceptionHandler { get; set; }
 66
 67        public static void AssertAndThrow(bool condition, string description)
 68        {
 069            if (!condition)
 70            {
 071                AssertAndThrow(description);
 72            }
 073        }
 74
 75        public static Exception AssertAndThrow(string description)
 76        {
 77            Assert(description);
 78            //TraceCore.ShipAssertExceptionMessage(Trace, description);
 079            throw new InternalException(description);
 80        }
 81
 82        [MethodImpl(MethodImplOptions.NoInlining)]
 83        public static Exception AssertAndThrowFatal(string description)
 84        {
 85            Assert(description);
 86            //TraceCore.ShipAssertExceptionMessage(Trace, description);
 087            throw new FatalInternalException(description);
 88        }
 89
 90        [Conditional("DEBUG")]
 91        public static void Assert(string description)
 92        {
 093            if (Debugger.IsAttached)
 94            {
 095                Debugger.Break();
 96            }
 97            Contract.Assert(false, description);
 098        }
 99
 100        [Conditional("DEBUG")]
 101        public static void Assert(bool condition, string description)
 102        {
 0103            if (!condition)
 104            {
 105                Assert(description);
 106            }
 0107        }
 108
 109        public static bool IsFatal(Exception exception)
 110        {
 0111            while (exception != null)
 112            {
 0113                if (exception is FatalException ||
 0114                    (exception is OutOfMemoryException /*&& !(exception is InsufficientMemoryException)*/) ||
 0115                    //exception is ThreadAbortException ||
 0116                    exception is FatalInternalException)
 117                {
 0118                    return true;
 119                }
 120
 121                // These exceptions aren't themselves fatal, but since the CLR uses them to wrap other exceptions,
 122                // we want to check to see whether they've been used to wrap a fatal exception.  If so, then they
 123                // count as fatal.
 0124                if (exception is TypeInitializationException ||
 0125                    exception is TargetInvocationException)
 126                {
 0127                    exception = exception.InnerException;
 128                }
 0129                else if (exception is AggregateException)
 130                {
 131                    // AggregateExceptions have a collection of inner exceptions, which may themselves be other
 132                    // wrapping exceptions (including nested AggregateExceptions).  Recursively walk this
 133                    // hierarchy.  The (singular) InnerException is included in the collection.
 0134                    ReadOnlyCollection<Exception> innerExceptions = ((AggregateException)exception).InnerExceptions;
 0135                    foreach (Exception innerException in innerExceptions)
 136                    {
 0137                        if (IsFatal(innerException))
 138                        {
 0139                            return true;
 140                        }
 141                    }
 142
 143                    break;
 144                }
 145                else
 146                {
 147                    break;
 148                }
 149            }
 150
 0151            return false;
 0152        }
 153
 154        [Serializable]
 155        internal class InternalException : SystemException
 156        {
 157            public InternalException(string description)
 0158                : base($"ShipAssertExceptionMessage,{description}" /*InternalSRCommon.ShipAssertExceptionMessage(descrip
 159            {
 0160            }
 161
 162            protected InternalException(SerializationInfo info, StreamingContext context)
 0163                : base(info, context)
 164            {
 0165            }
 166        }
 167
 168        [Serializable]
 169        internal class FatalInternalException : InternalException
 170        {
 171            public FatalInternalException(string description)
 0172                : base(description)
 173            {
 0174            }
 175
 176            protected FatalInternalException(SerializationInfo info, StreamingContext context)
 0177                : base(info, context)
 178            {
 0179            }
 180        }
 181
 182        internal static byte[] AllocateByteArray(int size)
 183        {
 184            try
 185            {
 186                // Safe to catch OOM from this as long as the ONLY thing it does is a simple allocation of a primitive t
 0187                return new byte[size];
 188            }
 0189            catch (OutOfMemoryException exception)
 190            {
 191                // Convert OOM into an exception that can be safely handled by higher layers.
 0192                throw Exception.AsError(exception);
 193                //new InsufficientMemoryException(InternalSRCommon.BufferAllocationFailed(size), exception));
 194            }
 0195        }
 196
 197        public static AsyncCallback ThunkCallback(AsyncCallback callback)
 198        {
 0199            return new AsyncThunk(callback).ThunkFrame;
 200        }
 201
 202        public static Action<T1> ThunkCallback<T1>(Action<T1> callback)
 203        {
 0204            return new ActionThunk<T1>(callback).ThunkFrame;
 205        }
 206
 207        public static Action<T1, T2> ThunkCallback<T1, T2>(Action<T1, T2> callback)
 208        {
 0209            return new ActionThunk<T1, T2>(callback).ThunkFrame;
 210        }
 211
 212        public static Action<T1, T2, T3> ThunkCallback<T1, T2, T3>(Action<T1, T2, T3> callback)
 213        {
 0214            return new ActionThunk<T1, T2, T3>(callback).ThunkFrame;
 215        }
 216
 217        [SupportedOSPlatform("windows")]
 218        public static IOCompletionCallback ThunkCallback(IOCompletionCallback callback)
 219        {
 220            Assert(callback != null, "Trying to create a ThunkCallback with a null callback method");
 0221            return (new IOCompletionThunk(callback)).ThunkFrame;
 222        }
 223
 224        private abstract class Thunk<T> where T : class
 225        {
 0226            protected Thunk(T callback)
 227            {
 0228                Callback = callback;
 0229            }
 230
 0231            internal T Callback { get; private set; }
 232        }
 233
 234        private sealed class ActionThunk<T1> : Thunk<Action<T1>>
 235        {
 0236            public ActionThunk(Action<T1> callback) : base(callback)
 237            {
 0238            }
 239
 240            public Action<T1> ThunkFrame
 241            {
 242                get
 243                {
 0244                    return UnhandledExceptionFrame;
 245                }
 246            }
 247
 248            private void UnhandledExceptionFrame(T1 param1)
 249            {
 250                try
 251                {
 0252                    Callback(param1);
 0253                }
 254                catch (Exception exception)
 255                {
 0256                    if (!HandleAtThreadBase(exception))
 257                    {
 0258                        throw;
 259                    }
 0260                }
 0261            }
 262        }
 263
 264        private sealed class ActionThunk<T1, T2> : Thunk<Action<T1, T2>>
 265        {
 0266            public ActionThunk(Action<T1, T2> callback) : base(callback)
 267            {
 0268            }
 269
 270            public Action<T1, T2> ThunkFrame
 271            {
 272                get
 273                {
 0274                    return UnhandledExceptionFrame;
 275                }
 276            }
 277
 278            private void UnhandledExceptionFrame(T1 param1, T2 param2)
 279            {
 280                try
 281                {
 0282                    Callback(param1, param2);
 0283                }
 284                catch (Exception exception)
 285                {
 0286                    if (!HandleAtThreadBase(exception))
 287                    {
 0288                        throw;
 289                    }
 0290                }
 0291            }
 292        }
 293
 294        private sealed class ActionThunk<T1, T2, T3> : Thunk<Action<T1, T2, T3>>
 295        {
 0296            public ActionThunk(Action<T1, T2, T3> callback) : base(callback)
 297            {
 0298            }
 299
 300            public Action<T1, T2, T3> ThunkFrame
 301            {
 302                get
 303                {
 0304                    return UnhandledExceptionFrame;
 305                }
 306            }
 307
 308            private void UnhandledExceptionFrame(T1 param1, T2 param2, T3 param3)
 309            {
 310                try
 311                {
 0312                    Callback(param1, param2, param3);
 0313                }
 314                catch (Exception exception)
 315                {
 0316                    if (!HandleAtThreadBase(exception))
 317                    {
 0318                        throw;
 319                    }
 0320                }
 0321            }
 322        }
 323
 324        private sealed class AsyncThunk : Thunk<AsyncCallback>
 325        {
 0326            public AsyncThunk(AsyncCallback callback) : base(callback)
 327            {
 0328            }
 329
 330            public AsyncCallback ThunkFrame
 331            {
 332                get
 333                {
 0334                    return new AsyncCallback(UnhandledExceptionFrame);
 335                }
 336            }
 337
 338            private void UnhandledExceptionFrame(IAsyncResult result)
 339            {
 340                // PrepareConstrainedRegions are in .net standard 1.7+
 341                //RuntimeHelpers.PrepareConstrainedRegions();
 342                try
 343                {
 0344                    Callback(result);
 0345                }
 346                catch (Exception exception)
 347                {
 0348                    if (!HandleAtThreadBase(exception))
 349                    {
 0350                        throw;
 351                    }
 0352                }
 0353            }
 354        }
 355
 356        private static void TraceExceptionNoThrow(Exception exception)
 357        {
 358            try
 359            {
 360                // This call exits the CER.  However, when still inside a catch, normal ThreadAbort is prevented.
 361                // Rude ThreadAbort will still be allowed to terminate processing.
 0362                Exception.TraceUnhandledException(exception);
 0363            }
 0364            catch
 365            {
 366                // This empty catch is only acceptable because we are a) in a CER and b) processing an exception
 367                // which is about to crash the process anyway.
 0368            }
 0369        }
 370
 371        private static bool HandleAtThreadBase(Exception exception)
 372        {
 373            // This area is too sensitive to do anything but return.
 0374            if (exception == null)
 375            {
 376                Assert("Null exception in HandleAtThreadBase.");
 0377                return false;
 378            }
 379
 0380            TraceExceptionNoThrow(exception);
 381
 382            try
 383            {
 0384                ExceptionHandler handler = AsynchronousThreadExceptionHandler;
 0385                return handler == null ? false : handler.HandleException(exception);
 386            }
 387            catch (Exception secondException)
 388            {
 389                // Don't let a new exception hide the original exception.
 0390                TraceExceptionNoThrow(secondException);
 0391            }
 392
 0393            return false;
 0394        }
 395
 396        public abstract class ExceptionHandler
 397        {
 398            public abstract bool HandleException(Exception exception);
 399        }
 400
 401        // This can't derive from Thunk since T would be unsafe.
 402        [SupportedOSPlatform("windows")]
 403        private sealed unsafe class IOCompletionThunk
 404        {
 405            private readonly IOCompletionCallback _callback;
 406
 0407            public IOCompletionThunk(IOCompletionCallback callback)
 408            {
 0409                _callback = callback;
 0410            }
 411
 412            public IOCompletionCallback ThunkFrame
 413            {
 414                get
 415                {
 0416                    return new IOCompletionCallback(UnhandledExceptionFrame);
 417                }
 418            }
 419
 420            private void UnhandledExceptionFrame(uint error, uint bytesRead, NativeOverlapped* nativeOverlapped)
 421            {
 422                try
 423                {
 0424                    _callback(error, bytesRead, nativeOverlapped);
 0425                }
 426                catch (Exception exception)
 427                {
 0428                    if (!HandleAtThreadBase(exception))
 429                    {
 0430                        throw;
 431                    }
 0432                }
 0433            }
 434        }
 435    }
 436}