| | | 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.Collections.ObjectModel; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | using System.Diagnostics.Contracts; |
| | | 8 | | using System.Reflection; |
| | | 9 | | using System.Runtime.CompilerServices; |
| | | 10 | | using System.Runtime.Serialization; |
| | | 11 | | using System.Threading; |
| | | 12 | | using CoreWCF.Runtime.Diagnostics; |
| | | 13 | | using System.Runtime.Versioning; |
| | | 14 | | |
| | | 15 | | namespace 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 | | { |
| | 0 | 27 | | if (s_exceptionTrace == null) |
| | | 28 | | { |
| | | 29 | | // don't need a lock here since a true singleton is not required |
| | 0 | 30 | | s_exceptionTrace = new ExceptionTrace(defaultEventSource, Trace); |
| | | 31 | | } |
| | | 32 | | |
| | 0 | 33 | | return s_exceptionTrace; |
| | | 34 | | } |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | public static EtwDiagnosticTrace Trace |
| | | 38 | | { |
| | | 39 | | get |
| | | 40 | | { |
| | 0 | 41 | | if (s_diagnosticTrace == null) |
| | | 42 | | { |
| | 0 | 43 | | s_diagnosticTrace = InitializeTracing(); |
| | | 44 | | } |
| | | 45 | | |
| | 0 | 46 | | return s_diagnosticTrace; |
| | | 47 | | } |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | private static EtwDiagnosticTrace InitializeTracing() |
| | | 51 | | { |
| | 0 | 52 | | 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); |
| | 0 | 62 | | return trace; |
| | | 63 | | } |
| | | 64 | | |
| | 0 | 65 | | public static ExceptionHandler AsynchronousThreadExceptionHandler { get; set; } |
| | | 66 | | |
| | | 67 | | public static void AssertAndThrow(bool condition, string description) |
| | | 68 | | { |
| | 0 | 69 | | if (!condition) |
| | | 70 | | { |
| | 0 | 71 | | AssertAndThrow(description); |
| | | 72 | | } |
| | 0 | 73 | | } |
| | | 74 | | |
| | | 75 | | public static Exception AssertAndThrow(string description) |
| | | 76 | | { |
| | | 77 | | Assert(description); |
| | | 78 | | //TraceCore.ShipAssertExceptionMessage(Trace, description); |
| | 0 | 79 | | 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); |
| | 0 | 87 | | throw new FatalInternalException(description); |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | [Conditional("DEBUG")] |
| | | 91 | | public static void Assert(string description) |
| | | 92 | | { |
| | 0 | 93 | | if (Debugger.IsAttached) |
| | | 94 | | { |
| | 0 | 95 | | Debugger.Break(); |
| | | 96 | | } |
| | | 97 | | Contract.Assert(false, description); |
| | 0 | 98 | | } |
| | | 99 | | |
| | | 100 | | [Conditional("DEBUG")] |
| | | 101 | | public static void Assert(bool condition, string description) |
| | | 102 | | { |
| | 0 | 103 | | if (!condition) |
| | | 104 | | { |
| | | 105 | | Assert(description); |
| | | 106 | | } |
| | 0 | 107 | | } |
| | | 108 | | |
| | | 109 | | public static bool IsFatal(Exception exception) |
| | | 110 | | { |
| | 0 | 111 | | while (exception != null) |
| | | 112 | | { |
| | 0 | 113 | | if (exception is FatalException || |
| | 0 | 114 | | (exception is OutOfMemoryException /*&& !(exception is InsufficientMemoryException)*/) || |
| | 0 | 115 | | //exception is ThreadAbortException || |
| | 0 | 116 | | exception is FatalInternalException) |
| | | 117 | | { |
| | 0 | 118 | | 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. |
| | 0 | 124 | | if (exception is TypeInitializationException || |
| | 0 | 125 | | exception is TargetInvocationException) |
| | | 126 | | { |
| | 0 | 127 | | exception = exception.InnerException; |
| | | 128 | | } |
| | 0 | 129 | | 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. |
| | 0 | 134 | | ReadOnlyCollection<Exception> innerExceptions = ((AggregateException)exception).InnerExceptions; |
| | 0 | 135 | | foreach (Exception innerException in innerExceptions) |
| | | 136 | | { |
| | 0 | 137 | | if (IsFatal(innerException)) |
| | | 138 | | { |
| | 0 | 139 | | return true; |
| | | 140 | | } |
| | | 141 | | } |
| | | 142 | | |
| | | 143 | | break; |
| | | 144 | | } |
| | | 145 | | else |
| | | 146 | | { |
| | | 147 | | break; |
| | | 148 | | } |
| | | 149 | | } |
| | | 150 | | |
| | 0 | 151 | | return false; |
| | 0 | 152 | | } |
| | | 153 | | |
| | | 154 | | [Serializable] |
| | | 155 | | internal class InternalException : SystemException |
| | | 156 | | { |
| | | 157 | | public InternalException(string description) |
| | 0 | 158 | | : base($"ShipAssertExceptionMessage,{description}" /*InternalSRCommon.ShipAssertExceptionMessage(descrip |
| | | 159 | | { |
| | 0 | 160 | | } |
| | | 161 | | |
| | | 162 | | protected InternalException(SerializationInfo info, StreamingContext context) |
| | 0 | 163 | | : base(info, context) |
| | | 164 | | { |
| | 0 | 165 | | } |
| | | 166 | | } |
| | | 167 | | |
| | | 168 | | [Serializable] |
| | | 169 | | internal class FatalInternalException : InternalException |
| | | 170 | | { |
| | | 171 | | public FatalInternalException(string description) |
| | 0 | 172 | | : base(description) |
| | | 173 | | { |
| | 0 | 174 | | } |
| | | 175 | | |
| | | 176 | | protected FatalInternalException(SerializationInfo info, StreamingContext context) |
| | 0 | 177 | | : base(info, context) |
| | | 178 | | { |
| | 0 | 179 | | } |
| | | 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 |
| | 0 | 187 | | return new byte[size]; |
| | | 188 | | } |
| | 0 | 189 | | catch (OutOfMemoryException exception) |
| | | 190 | | { |
| | | 191 | | // Convert OOM into an exception that can be safely handled by higher layers. |
| | 0 | 192 | | throw Exception.AsError(exception); |
| | | 193 | | //new InsufficientMemoryException(InternalSRCommon.BufferAllocationFailed(size), exception)); |
| | | 194 | | } |
| | 0 | 195 | | } |
| | | 196 | | |
| | | 197 | | public static AsyncCallback ThunkCallback(AsyncCallback callback) |
| | | 198 | | { |
| | 0 | 199 | | return new AsyncThunk(callback).ThunkFrame; |
| | | 200 | | } |
| | | 201 | | |
| | | 202 | | public static Action<T1> ThunkCallback<T1>(Action<T1> callback) |
| | | 203 | | { |
| | 0 | 204 | | return new ActionThunk<T1>(callback).ThunkFrame; |
| | | 205 | | } |
| | | 206 | | |
| | | 207 | | public static Action<T1, T2> ThunkCallback<T1, T2>(Action<T1, T2> callback) |
| | | 208 | | { |
| | 0 | 209 | | 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 | | { |
| | 0 | 214 | | 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"); |
| | 0 | 221 | | return (new IOCompletionThunk(callback)).ThunkFrame; |
| | | 222 | | } |
| | | 223 | | |
| | | 224 | | private abstract class Thunk<T> where T : class |
| | | 225 | | { |
| | 0 | 226 | | protected Thunk(T callback) |
| | | 227 | | { |
| | 0 | 228 | | Callback = callback; |
| | 0 | 229 | | } |
| | | 230 | | |
| | 0 | 231 | | internal T Callback { get; private set; } |
| | | 232 | | } |
| | | 233 | | |
| | | 234 | | private sealed class ActionThunk<T1> : Thunk<Action<T1>> |
| | | 235 | | { |
| | 0 | 236 | | public ActionThunk(Action<T1> callback) : base(callback) |
| | | 237 | | { |
| | 0 | 238 | | } |
| | | 239 | | |
| | | 240 | | public Action<T1> ThunkFrame |
| | | 241 | | { |
| | | 242 | | get |
| | | 243 | | { |
| | 0 | 244 | | return UnhandledExceptionFrame; |
| | | 245 | | } |
| | | 246 | | } |
| | | 247 | | |
| | | 248 | | private void UnhandledExceptionFrame(T1 param1) |
| | | 249 | | { |
| | | 250 | | try |
| | | 251 | | { |
| | 0 | 252 | | Callback(param1); |
| | 0 | 253 | | } |
| | | 254 | | catch (Exception exception) |
| | | 255 | | { |
| | 0 | 256 | | if (!HandleAtThreadBase(exception)) |
| | | 257 | | { |
| | 0 | 258 | | throw; |
| | | 259 | | } |
| | 0 | 260 | | } |
| | 0 | 261 | | } |
| | | 262 | | } |
| | | 263 | | |
| | | 264 | | private sealed class ActionThunk<T1, T2> : Thunk<Action<T1, T2>> |
| | | 265 | | { |
| | 0 | 266 | | public ActionThunk(Action<T1, T2> callback) : base(callback) |
| | | 267 | | { |
| | 0 | 268 | | } |
| | | 269 | | |
| | | 270 | | public Action<T1, T2> ThunkFrame |
| | | 271 | | { |
| | | 272 | | get |
| | | 273 | | { |
| | 0 | 274 | | return UnhandledExceptionFrame; |
| | | 275 | | } |
| | | 276 | | } |
| | | 277 | | |
| | | 278 | | private void UnhandledExceptionFrame(T1 param1, T2 param2) |
| | | 279 | | { |
| | | 280 | | try |
| | | 281 | | { |
| | 0 | 282 | | Callback(param1, param2); |
| | 0 | 283 | | } |
| | | 284 | | catch (Exception exception) |
| | | 285 | | { |
| | 0 | 286 | | if (!HandleAtThreadBase(exception)) |
| | | 287 | | { |
| | 0 | 288 | | throw; |
| | | 289 | | } |
| | 0 | 290 | | } |
| | 0 | 291 | | } |
| | | 292 | | } |
| | | 293 | | |
| | | 294 | | private sealed class ActionThunk<T1, T2, T3> : Thunk<Action<T1, T2, T3>> |
| | | 295 | | { |
| | 0 | 296 | | public ActionThunk(Action<T1, T2, T3> callback) : base(callback) |
| | | 297 | | { |
| | 0 | 298 | | } |
| | | 299 | | |
| | | 300 | | public Action<T1, T2, T3> ThunkFrame |
| | | 301 | | { |
| | | 302 | | get |
| | | 303 | | { |
| | 0 | 304 | | return UnhandledExceptionFrame; |
| | | 305 | | } |
| | | 306 | | } |
| | | 307 | | |
| | | 308 | | private void UnhandledExceptionFrame(T1 param1, T2 param2, T3 param3) |
| | | 309 | | { |
| | | 310 | | try |
| | | 311 | | { |
| | 0 | 312 | | Callback(param1, param2, param3); |
| | 0 | 313 | | } |
| | | 314 | | catch (Exception exception) |
| | | 315 | | { |
| | 0 | 316 | | if (!HandleAtThreadBase(exception)) |
| | | 317 | | { |
| | 0 | 318 | | throw; |
| | | 319 | | } |
| | 0 | 320 | | } |
| | 0 | 321 | | } |
| | | 322 | | } |
| | | 323 | | |
| | | 324 | | private sealed class AsyncThunk : Thunk<AsyncCallback> |
| | | 325 | | { |
| | 0 | 326 | | public AsyncThunk(AsyncCallback callback) : base(callback) |
| | | 327 | | { |
| | 0 | 328 | | } |
| | | 329 | | |
| | | 330 | | public AsyncCallback ThunkFrame |
| | | 331 | | { |
| | | 332 | | get |
| | | 333 | | { |
| | 0 | 334 | | 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 | | { |
| | 0 | 344 | | Callback(result); |
| | 0 | 345 | | } |
| | | 346 | | catch (Exception exception) |
| | | 347 | | { |
| | 0 | 348 | | if (!HandleAtThreadBase(exception)) |
| | | 349 | | { |
| | 0 | 350 | | throw; |
| | | 351 | | } |
| | 0 | 352 | | } |
| | 0 | 353 | | } |
| | | 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. |
| | 0 | 362 | | Exception.TraceUnhandledException(exception); |
| | 0 | 363 | | } |
| | 0 | 364 | | 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. |
| | 0 | 368 | | } |
| | 0 | 369 | | } |
| | | 370 | | |
| | | 371 | | private static bool HandleAtThreadBase(Exception exception) |
| | | 372 | | { |
| | | 373 | | // This area is too sensitive to do anything but return. |
| | 0 | 374 | | if (exception == null) |
| | | 375 | | { |
| | | 376 | | Assert("Null exception in HandleAtThreadBase."); |
| | 0 | 377 | | return false; |
| | | 378 | | } |
| | | 379 | | |
| | 0 | 380 | | TraceExceptionNoThrow(exception); |
| | | 381 | | |
| | | 382 | | try |
| | | 383 | | { |
| | 0 | 384 | | ExceptionHandler handler = AsynchronousThreadExceptionHandler; |
| | 0 | 385 | | return handler == null ? false : handler.HandleException(exception); |
| | | 386 | | } |
| | | 387 | | catch (Exception secondException) |
| | | 388 | | { |
| | | 389 | | // Don't let a new exception hide the original exception. |
| | 0 | 390 | | TraceExceptionNoThrow(secondException); |
| | 0 | 391 | | } |
| | | 392 | | |
| | 0 | 393 | | return false; |
| | 0 | 394 | | } |
| | | 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 | | |
| | 0 | 407 | | public IOCompletionThunk(IOCompletionCallback callback) |
| | | 408 | | { |
| | 0 | 409 | | _callback = callback; |
| | 0 | 410 | | } |
| | | 411 | | |
| | | 412 | | public IOCompletionCallback ThunkFrame |
| | | 413 | | { |
| | | 414 | | get |
| | | 415 | | { |
| | 0 | 416 | | return new IOCompletionCallback(UnhandledExceptionFrame); |
| | | 417 | | } |
| | | 418 | | } |
| | | 419 | | |
| | | 420 | | private void UnhandledExceptionFrame(uint error, uint bytesRead, NativeOverlapped* nativeOverlapped) |
| | | 421 | | { |
| | | 422 | | try |
| | | 423 | | { |
| | 0 | 424 | | _callback(error, bytesRead, nativeOverlapped); |
| | 0 | 425 | | } |
| | | 426 | | catch (Exception exception) |
| | | 427 | | { |
| | 0 | 428 | | if (!HandleAtThreadBase(exception)) |
| | | 429 | | { |
| | 0 | 430 | | throw; |
| | | 431 | | } |
| | 0 | 432 | | } |
| | 0 | 433 | | } |
| | | 434 | | } |
| | | 435 | | } |
| | | 436 | | } |