From f64f4369f60b111d21a600ad1d71cef0e0f88347 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Wed, 27 Jan 2021 12:53:42 +0100 Subject: [PATCH 1/4] Avoid Marshal.SizeOf in EventSource We're getting the marshalling size of an enum underlying type - there are not that many of those. I chose not to treat `char` and `bool`, but we could (the only other remaining options). Calling into `Marshal.SizeOf` increases the risk that `EventSource` is going call itself recursively - see the comment at the beginning of the method. This was originally introduced in https://github.com/dotnet/coreclr/pull/19205. --- .../src/System/Diagnostics/Tracing/EventSource.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs index 2492b0161b4d33..80801fa398e5e3 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs @@ -1865,9 +1865,11 @@ private static Guid GenerateGuidFromName(string name) { dataType = Enum.GetUnderlyingType(dataType); - int dataTypeSize = System.Runtime.InteropServices.Marshal.SizeOf(dataType); - if (dataTypeSize < sizeof(int)) + if (dataType == typeof(byte) || dataType == typeof(sbyte) + || dataType == typeof(ushort) || dataType == typeof(short)) + { dataType = typeof(int); + } goto Again; } From a37f0cb46d6a7005404065c06f3140d3e05b952e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Wed, 27 Jan 2021 16:25:29 +0100 Subject: [PATCH 2/4] Update src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs --- .../src/System/Diagnostics/Tracing/EventSource.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs index 80801fa398e5e3..560ae4d54580db 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs @@ -1866,6 +1866,7 @@ private static Guid GenerateGuidFromName(string name) dataType = Enum.GetUnderlyingType(dataType); if (dataType == typeof(byte) || dataType == typeof(sbyte) + || dataType == typeof(bool) || dataType == typeof(char) || dataType == typeof(ushort) || dataType == typeof(short)) { dataType = typeof(int); From 797cbd887b75ef55580f6da05ef3a511cd401fd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Wed, 27 Jan 2021 16:43:14 +0100 Subject: [PATCH 3/4] Update src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs --- .../src/System/Diagnostics/Tracing/EventSource.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs index 560ae4d54580db..80801fa398e5e3 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs @@ -1866,7 +1866,6 @@ private static Guid GenerateGuidFromName(string name) dataType = Enum.GetUnderlyingType(dataType); if (dataType == typeof(byte) || dataType == typeof(sbyte) - || dataType == typeof(bool) || dataType == typeof(char) || dataType == typeof(ushort) || dataType == typeof(short)) { dataType = typeof(int); From d0f37e8be697d892d3129bca8103ca7cbaeb79b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Thu, 28 Jan 2021 09:20:37 +0100 Subject: [PATCH 4/4] Update src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs --- .../src/System/Diagnostics/Tracing/EventSource.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs index 80801fa398e5e3..b5e053e46595ed 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs @@ -1865,10 +1865,15 @@ private static Guid GenerateGuidFromName(string name) { dataType = Enum.GetUnderlyingType(dataType); - if (dataType == typeof(byte) || dataType == typeof(sbyte) - || dataType == typeof(ushort) || dataType == typeof(short)) + // Enums less than 4 bytes in size should be treated as int. + switch (Type.GetTypeCode(dataType)) { - dataType = typeof(int); + case TypeCode.Byte: + case TypeCode.SByte: + case TypeCode.Int16: + case TypeCode.UInt16: + dataType = typeof(int); + break; } goto Again; }