Skip to content
This repository was archived by the owner on Nov 1, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Common/src/Internal/Text/Utf8StringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Text;
using System.Diagnostics;

namespace Internal.Text
{
Expand All @@ -26,6 +27,13 @@ public Utf8StringBuilder Clear()
return this;
}

public Utf8StringBuilder Truncate(int newLength)
{
Debug.Assert(newLength <= _length);
_length = newLength;
return this;
}

public Utf8StringBuilder Append(Utf8String value)
{
return Append(value.UnderlyingArray);
Expand Down
50 changes: 1 addition & 49 deletions src/ILCompiler.Compiler/src/Compiler/UtcNameMangler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,6 @@ public class UTCNameMangler : NameMangler
private string OrdinalPrefix => "_[O]_";
private string deDuplicatePrefix => "_[D]_";

// The max length of name allowed in terms of UTF8 string. This is the limit from the compiler.
private int _maximumUTF8NameLength = 4000;

// The max length of prefix+suffix added to the name in terms of UTF8 string. This is usually for generated
// hash code, node-specific prefixes and suffixes, and ordinal prefixes.
private int _nameUTF8MarginLength = 1000;

#if DEBUG
ConcurrentDictionary<string, string> _longNames = new ConcurrentDictionary<string, string>();
#endif

private ImportExportOrdinals _importOrdinals;
private ImportExportOrdinals _exportOrdinals;

Expand Down Expand Up @@ -241,33 +230,7 @@ private string DisambiguateName(string origName, ISet<string> set)
result = string.Concat(origName, deDuplicatePrefix, (iter++).ToStringInvariant());
}
return result;
}

private string TruncateName(string origName)
{
// A UTF8 char can only expand to 3 bytes
if (origName.Length * 3 <= _maximumUTF8NameLength)
{
return origName;
}

// Compute the exact UTF8 length and compare
if (Encoding.UTF8.GetBytes(origName).Length <= _maximumUTF8NameLength)
{
return origName;
}

var hash = _sha256.ComputeHash(GetBytesFromString(origName));
var truncatedName = origName.Substring(0, _maximumUTF8NameLength - _nameUTF8MarginLength);
truncatedName = truncatedName + BitConverter.ToString(hash).Replace("-", "");

#if DEBUG
string existingName = _longNames.GetOrAdd(truncatedName, origName);
Debug.Assert(existingName == origName);
#endif

return truncatedName;
}
}

public override string GetMangledTypeName(TypeDesc type)
{
Expand Down Expand Up @@ -328,7 +291,6 @@ private string ComputeMangledTypeName(TypeDesc type)
// Ensure that name is unique and update our tables accordingly.
name = DisambiguateName(name, deduplicator);
deduplicator.Add(name);
name = TruncateName(name);
_mangledTypeNames = _mangledTypeNames.Add(t, name);
}
}
Expand Down Expand Up @@ -390,8 +352,6 @@ private string ComputeMangledTypeName(TypeDesc type)
break;
}

mangledName = TruncateName(mangledName);

lock (this)
{
// Ensure that name is unique and update our tables accordingly.
Expand Down Expand Up @@ -487,8 +447,6 @@ private Utf8String ComputeMangledNameMethodWithoutInstantiation(MethodDesc metho

if (prependTypeName != null)
name = prependTypeName + "__" + name;

name = TruncateName(name);
}

_mangledMethodNames = _mangledMethodNames.Add(m, name);
Expand Down Expand Up @@ -535,8 +493,6 @@ private Utf8String ComputeMangledMethodName(MethodDesc method)
mangledName += mangledInstantiation;
}

mangledName = TruncateName(mangledName);

lock (this)
{
utf8MangledName = new Utf8String(mangledName);
Expand Down Expand Up @@ -613,8 +569,6 @@ private Utf8String ComputeMangledFieldName(FieldDesc field)
if (prependTypeName != null)
name = prependTypeName + "__" + name;

name = TruncateName(name);

_mangledFieldNames = _mangledFieldNames.Add(f, name);
}
}
Expand All @@ -629,8 +583,6 @@ private Utf8String ComputeMangledFieldName(FieldDesc field)
if (prependTypeName != null)
mangledName = prependTypeName + "__" + mangledName;

mangledName = TruncateName(mangledName);

Utf8String utf8MangledName = new Utf8String(mangledName);

lock (this)
Expand Down
51 changes: 19 additions & 32 deletions src/Runtime.Base/src/System/Runtime/ExceptionHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@

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
Expand Down Expand Up @@ -183,7 +180,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 when (true)
catch
{
// disallow all exceptions leaking out of callbacks
}
Expand Down Expand Up @@ -229,7 +226,7 @@ private static void OnFirstChanceExceptionViaClassLib(object exception)
{
CalliIntrinsics.CallVoid(pOnFirstChanceFunction, exception);
}
catch when (true)
catch
{
// disallow all exceptions leaking out of callbacks
}
Expand Down Expand Up @@ -261,7 +258,7 @@ internal static unsafe void UnhandledExceptionFailFastViaClasslib(
{
CalliIntrinsics.CallVoid(pFailFastFunction, reason, unhandledException, exInfo._pExContext->IP, (IntPtr)pContext);
}
catch when (true)
catch
{
// disallow all exceptions leaking out of callbacks
}
Expand Down Expand Up @@ -291,7 +288,7 @@ private static void AppendExceptionStackFrameViaClasslib(object exception, IntPt
{
CalliIntrinsics.CallVoid(pAppendStackFrame, exception, IP, flags);
}
catch when (true)
catch
{
// disallow all exceptions leaking out of callbacks
}
Expand All @@ -318,7 +315,7 @@ internal static Exception GetClasslibException(ExceptionIDs id, IntPtr address)
{
e = CalliIntrinsics.Call<Exception>(pGetRuntimeExceptionFunction, id);
}
catch when (true)
catch
{
// disallow all exceptions leaking out of callbacks
}
Expand Down Expand Up @@ -354,7 +351,7 @@ internal static Exception GetClasslibExceptionFromEEType(ExceptionIDs id, IntPtr
{
e = CalliIntrinsics.Call<Exception>(pGetRuntimeExceptionFunction, id);
}
catch when (true)
catch
{
// disallow all exceptions leaking out of callbacks
}
Expand Down Expand Up @@ -876,38 +873,28 @@ private static bool FindFirstPassHandler(object exception, uint idxStart,
return false;
}

#if DEBUG && !INPLACE_RUNTIME
private static EEType* s_pLowLevelObjectType;
private static void AssertNotRuntimeObject(EEType* pClauseType)

private static bool ShouldTypedClauseCatchThisException(object exception, EEType* pClauseType)
{
//
// 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 (TypeCast.IsInstanceOfClass(exception, pClauseType) != null)
return true;

if (s_pLowLevelObjectType == null)
{
// Allocating might fail, but since this is just a debug assert, it's probably fine.
// TODO: Avoid allocating here as that may fail
s_pLowLevelObjectType = new System.Object().EEType;
}

Debug.Assert(!pClauseType->IsEquivalentTo(s_pLowLevelObjectType));
}
#endif // DEBUG && !INPLACE_RUNTIME
// 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;


private static bool ShouldTypedClauseCatchThisException(object exception, EEType* pClauseType)
{
#if DEBUG && !INPLACE_RUNTIME
AssertNotRuntimeObject(pClauseType);
#endif

return TypeCast.IsInstanceOfClass(exception, pClauseType) != null;
return false;
}

private static void InvokeSecondPass(ref ExInfo exInfo, uint idxStart)
Expand Down
4 changes: 2 additions & 2 deletions src/Runtime.Base/src/System/Runtime/ThunkPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public static unsafe ThunksHeap CreateThunksHeap(IntPtr commonStubAddress)
if (newHeap._nextAvailableThunkPtr != IntPtr.Zero)
return newHeap;
}
catch (Exception) { }
catch { }

return null;
}
Expand All @@ -156,7 +156,7 @@ private unsafe bool ExpandHeap()
{
newBlockInfo = new AllocatedBlock();
}
catch (Exception)
catch
{
return false;
}
Expand Down