diff --git a/src/ILCompiler.Compiler/src/Compiler/DependencyAnalysis/UtcNodeFactory.cs b/src/ILCompiler.Compiler/src/Compiler/DependencyAnalysis/UtcNodeFactory.cs index 3aa15b2144a..a5be2575790 100644 --- a/src/ILCompiler.Compiler/src/Compiler/DependencyAnalysis/UtcNodeFactory.cs +++ b/src/ILCompiler.Compiler/src/Compiler/DependencyAnalysis/UtcNodeFactory.cs @@ -11,6 +11,7 @@ using ILCompiler.DependencyAnalysisFramework; using Internal.Runtime; using Internal.TypeSystem; +using Internal.TypeSystem.Ecma; namespace ILCompiler { @@ -339,6 +340,13 @@ public StandaloneGCStaticDescRegionNode StandaloneGCStaticDescRegion(GCStaticDes return _standaloneGCStaticDescs.GetOrAdd(staticDesc); } + public BlobNode FieldRvaDataBlob(FieldDesc field) + { + // Use the typical field definition in case this is an instantiated generic type + field = field.GetTypicalFieldDefinition(); + return ReadOnlyDataBlob(NameMangler.GetMangledFieldName(field), ((EcmaField)field).GetFieldRvaData(), Target.PointerSize); + } + public class UtcDictionaryLayoutProvider : DictionaryLayoutProvider { public override DictionaryLayoutNode GetLayout(TypeSystemEntity methodOrType) diff --git a/src/ILCompiler.Compiler/src/Compiler/UtcNameMangler.cs b/src/ILCompiler.Compiler/src/Compiler/UtcNameMangler.cs index 82a4eb2b5c9..45e32e38c25 100644 --- a/src/ILCompiler.Compiler/src/Compiler/UtcNameMangler.cs +++ b/src/ILCompiler.Compiler/src/Compiler/UtcNameMangler.cs @@ -661,10 +661,9 @@ public override string GetMangledStringName(string literal) return mangledName; } - public string GetMangledDataBlobName(byte[] blob) + public string GetMangledDataBlobName(FieldDesc field) { - var hash = _sha256.ComputeHash(blob); - return "__Data_" + BitConverter.ToString(hash).Replace("-", ""); + return "__Data_" + ComputeMangledFieldName(field); } public string GetImportedTlsIndexPrefix() diff --git a/src/Runtime.Base/src/System/Runtime/ExceptionHandling.cs b/src/Runtime.Base/src/System/Runtime/ExceptionHandling.cs index 26756641fb2..df3b910293c 100644 --- a/src/Runtime.Base/src/System/Runtime/ExceptionHandling.cs +++ b/src/Runtime.Base/src/System/Runtime/ExceptionHandling.cs @@ -9,6 +9,9 @@ using Internal.Runtime; +// Disable: Filter expression is a constant. We know. We just can't do an unfiltered catch. +#pragma warning disable 7095 + namespace System.Runtime { public enum RhFailFastReason @@ -180,7 +183,7 @@ internal static void FailFastViaClasslib(RhFailFastReason reason, object unhandl // Invoke the classlib fail fast function. CalliIntrinsics.CallVoid(pFailFastFunction, reason, unhandledException, IntPtr.Zero, IntPtr.Zero); } - catch + catch when (true) { // disallow all exceptions leaking out of callbacks } @@ -226,7 +229,7 @@ private static void OnFirstChanceExceptionViaClassLib(object exception) { CalliIntrinsics.CallVoid(pOnFirstChanceFunction, exception); } - catch + catch when (true) { // disallow all exceptions leaking out of callbacks } @@ -258,7 +261,7 @@ internal static unsafe void UnhandledExceptionFailFastViaClasslib( { CalliIntrinsics.CallVoid(pFailFastFunction, reason, unhandledException, exInfo._pExContext->IP, (IntPtr)pContext); } - catch + catch when (true) { // disallow all exceptions leaking out of callbacks } @@ -288,7 +291,7 @@ private static void AppendExceptionStackFrameViaClasslib(object exception, IntPt { CalliIntrinsics.CallVoid(pAppendStackFrame, exception, IP, flags); } - catch + catch when (true) { // disallow all exceptions leaking out of callbacks } @@ -315,7 +318,7 @@ internal static Exception GetClasslibException(ExceptionIDs id, IntPtr address) { e = CalliIntrinsics.Call(pGetRuntimeExceptionFunction, id); } - catch + catch when (true) { // disallow all exceptions leaking out of callbacks } @@ -351,7 +354,7 @@ internal static Exception GetClasslibExceptionFromEEType(ExceptionIDs id, IntPtr { e = CalliIntrinsics.Call(pGetRuntimeExceptionFunction, id); } - catch + catch when (true) { // disallow all exceptions leaking out of callbacks } @@ -873,28 +876,38 @@ private static bool FindFirstPassHandler(object exception, uint idxStart, return false; } +#if DEBUG && !INPLACE_RUNTIME private static EEType* s_pLowLevelObjectType; - - private static bool ShouldTypedClauseCatchThisException(object exception, EEType* pClauseType) + private static void AssertNotRuntimeObject(EEType* pClauseType) { - if (TypeCast.IsInstanceOfClass(exception, pClauseType) != null) - return true; + // + // The C# try { } catch { } clause expands into a typed catch of System.Object. + // Since runtime has its own definition of System.Object, try { } catch { } might not do what + // was intended (catch all exceptions). + // + // This assertion is making sure we don't use try { } catch { } within the runtime. + // The runtime codebase should either use try { } catch (Exception) { } for exception types + // from the runtime or a try { } catch when (true) { } to catch all exceptions. + // if (s_pLowLevelObjectType == null) { - // TODO: Avoid allocating here as that may fail + // Allocating might fail, but since this is just a debug assert, it's probably fine. s_pLowLevelObjectType = new System.Object().EEType; } - // This allows the typical try { } catch { }--which expands to a typed catch of System.Object--to work on - // all objects when the clause is in the low level runtime code. This special case is needed because - // objects from foreign type systems are sometimes throw back up at runtime code and this is the only way - // to catch them outside of having a filter with no type check in it, which isn't currently possible to - // write in C#. See https://github.com/dotnet/roslyn/issues/4388 - if (pClauseType->IsEquivalentTo(s_pLowLevelObjectType)) - return true; + Debug.Assert(!pClauseType->IsEquivalentTo(s_pLowLevelObjectType)); + } +#endif // DEBUG && !INPLACE_RUNTIME - return false; + + private static bool ShouldTypedClauseCatchThisException(object exception, EEType* pClauseType) + { +#if DEBUG && !INPLACE_RUNTIME + AssertNotRuntimeObject(pClauseType); +#endif + + return TypeCast.IsInstanceOfClass(exception, pClauseType) != null; } private static void InvokeSecondPass(ref ExInfo exInfo, uint idxStart) diff --git a/src/Runtime.Base/src/System/Runtime/ThunkPool.cs b/src/Runtime.Base/src/System/Runtime/ThunkPool.cs index 404494451eb..76df73e4dc6 100644 --- a/src/Runtime.Base/src/System/Runtime/ThunkPool.cs +++ b/src/Runtime.Base/src/System/Runtime/ThunkPool.cs @@ -135,7 +135,7 @@ public static unsafe ThunksHeap CreateThunksHeap(IntPtr commonStubAddress) if (newHeap._nextAvailableThunkPtr != IntPtr.Zero) return newHeap; } - catch { } + catch (Exception) { } return null; } @@ -156,7 +156,7 @@ private unsafe bool ExpandHeap() { newBlockInfo = new AllocatedBlock(); } - catch + catch (Exception) { return false; }