diff options
author | Jan Vorlicek <janvorli@microsoft.com> | 2018-03-06 04:21:09 +0300 |
---|---|---|
committer | Jan Kotas <jkotas@microsoft.com> | 2018-03-06 09:29:21 +0300 |
commit | 68679f4350785a333c33a9a9f21bf4911ed49d22 (patch) | |
tree | b3c77db5523f5a5715b2ceaa28a23d7ff86f5a7c | |
parent | ce9cd701b13774174765f731834fb2cb1cf8ebfc (diff) |
Remove libuuid usage (dotnet/coreclr#16643)
* Remove libuuid usage
This change removes dependency on the libuuid library that is used for GUID creation only.
It implements it using a random generator instead.
It also modifies return type of PAL_Random to VOID since it was always
returning TRUE and none of the existing callers were checking it.
1. Port the GUID creation to managed code.
2. Modify the PAL_Random to have 6 times better perf so that the perf of the
CoCreateGuid that is used in the native runtime doesn't degrade that much
w.r.t the previous state when the libuuid was used.
3. Use Interop.GetRandomBytes on Unix and fix Windows
Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
4 files changed, 85 insertions, 0 deletions
diff --git a/src/System.Private.CoreLib/shared/Interop/Windows/Ole32/Interop.CoCreateGuid.cs b/src/System.Private.CoreLib/shared/Interop/Windows/Ole32/Interop.CoCreateGuid.cs new file mode 100644 index 000000000..60cfb23ea --- /dev/null +++ b/src/System.Private.CoreLib/shared/Interop/Windows/Ole32/Interop.CoCreateGuid.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class Ole32 + { + [DllImport(Interop.Libraries.Ole32)] + internal extern static int CoCreateGuid(out Guid guid); + } +} diff --git a/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems b/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems index 46a161e00..a0c6c0cdc 100644 --- a/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems +++ b/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems @@ -701,6 +701,7 @@ <Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Normaliz\Interop.Idna.cs" Condition="'$(EnableDummyGlobalizationImplementation)' != 'true'" /> <Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Normaliz\Interop.Normalization.cs" Condition="'$(EnableDummyGlobalizationImplementation)' != 'true'" /> <Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.TimeZone.cs" /> + <Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Ole32\Interop.CoCreateGuid.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\OleAut32\Interop.SysAllocStringLen.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\OleAut32\Interop.SysFreeString.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\OleAut32\Interop.SysStringLen.cs" /> @@ -716,6 +717,7 @@ <Compile Include="$(MSBuildThisFileDirectory)System\Globalization\JapaneseCalendar.WinRT.cs" Condition="'$(EnableWinRT)' == 'true'" /> <Compile Include="$(MSBuildThisFileDirectory)System\Globalization\Normalization.Windows.cs" Condition="'$(EnableDummyGlobalizationImplementation)' != 'true'" /> <Compile Include="$(MSBuildThisFileDirectory)System\Globalization\TextInfo.Windows.cs" Condition="'$(EnableDummyGlobalizationImplementation)' != 'true'" /> + <Compile Include="$(MSBuildThisFileDirectory)System\Guid.Windows.cs" /> <Compile Include="$(MSBuildThisFileDirectory)System\IO\FileStream.Windows.cs" /> <Compile Include="$(MSBuildThisFileDirectory)System\IO\FileStreamCompletionSource.Win32.cs" /> <Compile Include="$(MSBuildThisFileDirectory)System\IO\Path.Windows.cs" /> @@ -795,6 +797,7 @@ <Compile Include="$(MSBuildThisFileDirectory)System\Globalization\LocaleData.Unix.cs" Condition="'$(EnableDummyGlobalizationImplementation)' != 'true'" /> <Compile Include="$(MSBuildThisFileDirectory)System\Globalization\Normalization.Unix.cs" Condition="'$(EnableDummyGlobalizationImplementation)' != 'true'" /> <Compile Include="$(MSBuildThisFileDirectory)System\Globalization\TextInfo.Unix.cs" Condition="'$(EnableDummyGlobalizationImplementation)' != 'true'" /> + <Compile Include="$(MSBuildThisFileDirectory)System\Guid.Unix.cs" /> <Compile Include="$(MSBuildThisFileDirectory)System\IO\FileStream.OSX.cs" Condition="'$(TargetsOSX)' == 'true'" /> <Compile Include="$(MSBuildThisFileDirectory)System\IO\FileStream.Linux.cs" Condition="'$(TargetsOSX)' != 'true'" /> <Compile Include="$(MSBuildThisFileDirectory)System\IO\FileStream.Unix.cs" /> diff --git a/src/System.Private.CoreLib/shared/System/Guid.Unix.cs b/src/System.Private.CoreLib/shared/System/Guid.Unix.cs new file mode 100644 index 000000000..442e7f883 --- /dev/null +++ b/src/System.Private.CoreLib/shared/System/Guid.Unix.cs @@ -0,0 +1,38 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Diagnostics; +using System.Runtime.InteropServices; + +namespace System +{ + partial struct Guid + { + // This will create a new random guid based on the https://www.ietf.org/rfc/rfc4122.txt + public static unsafe Guid NewGuid() + { + Guid g; + Interop.GetRandomBytes((byte*)&g, sizeof(Guid)); + + const ushort VersionMask = 0xF000; + const ushort RandomGuidVersion = 0x4000; + + const byte ClockSeqHiAndReservedMask = 0xC0; + const byte ClockSeqHiAndReservedValue = 0x80; + + // Modify bits indicating the type of the GUID + + unchecked + { + // time_hi_and_version + g._c = (short)((g._c & ~VersionMask) | RandomGuidVersion); + // clock_seq_hi_and_reserved + g._d = (byte)((g._d & ~ClockSeqHiAndReservedMask) | ClockSeqHiAndReservedValue); + } + + return g; + } + } +} + diff --git a/src/System.Private.CoreLib/shared/System/Guid.Windows.cs b/src/System.Private.CoreLib/shared/System/Guid.Windows.cs new file mode 100644 index 000000000..f00fbe45b --- /dev/null +++ b/src/System.Private.CoreLib/shared/System/Guid.Windows.cs @@ -0,0 +1,29 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System +{ + partial struct Guid + { + public static Guid NewGuid() + { + // CoCreateGuid should never return Guid.Empty, since it attempts to maintain some + // uniqueness guarantees. + + Guid g; + int hr = Interop.Ole32.CoCreateGuid(out g); + // We don't expect that this will ever throw an error, none are even documented, and so we don't want to pull + // in the HR to ComException mappings into the core library just for this so we will try a generic exception if + // we ever hit this condition. + if (hr != 0) + { + Exception ex = new Exception(); + ex.SetErrorCode(hr); + throw ex; + } + return g; + } + } +} + |