summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJan Vorlicek <janvorli@microsoft.com>2018-03-06 02:21:09 +0100
committerGitHub <noreply@github.com>2018-03-06 02:21:09 +0100
commit76245a33daf1e4a8dc724e9b7fd712bd41a7aee0 (patch)
tree5067dae50136e8caca443e2d749814e05f653e3f /src
parenta2628fc507df0c011383dd0c275bfb0d51bcd060 (diff)
downloadcoreclr-76245a33daf1e4a8dc724e9b7fd712bd41a7aee0.tar.gz
coreclr-76245a33daf1e4a8dc724e9b7fd712bd41a7aee0.tar.bz2
coreclr-76245a33daf1e4a8dc724e9b7fd712bd41a7aee0.zip
Remove libuuid usage (#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
Diffstat (limited to 'src')
-rw-r--r--src/ToolBox/superpmi/superpmi-shim-collector/superpmi-shim-collector.cpp2
-rw-r--r--src/ToolBox/superpmi/superpmi-shim-counter/methodcallsummarizer.cpp2
-rw-r--r--src/ToolBox/superpmi/superpmi/parallelsuperpmi.cpp2
-rw-r--r--src/dlls/mscordac/mscordac_unixexports.src2
-rw-r--r--src/dlls/mscoree/mscorwks_unixexports.src1
-rw-r--r--src/mscorlib/System.Private.CoreLib.csproj1
-rw-r--r--src/mscorlib/shared/Interop/Windows/Ole32/Interop.CoCreateGuid.cs15
-rw-r--r--src/mscorlib/shared/System.Private.CoreLib.Shared.projitems3
-rw-r--r--src/mscorlib/shared/System/Guid.Unix.cs38
-rw-r--r--src/mscorlib/shared/System/Guid.Windows.cs29
-rw-r--r--src/mscorlib/src/Microsoft/Win32/Win32Native.cs3
-rw-r--r--src/mscorlib/src/System/Guid.CoreCLR.cs19
-rw-r--r--src/pal/inc/pal.h8
-rw-r--r--src/pal/inc/rt/palrt.h3
-rw-r--r--src/pal/src/CMakeLists.txt1
-rw-r--r--src/pal/src/config.h.in2
-rw-r--r--src/pal/src/configure.cmake31
-rw-r--r--src/pal/src/misc/miscpalapi.cpp120
-rw-r--r--src/palrt/guid.cpp22
-rw-r--r--src/vm/comutilnative.cpp2
20 files changed, 143 insertions, 163 deletions
diff --git a/src/ToolBox/superpmi/superpmi-shim-collector/superpmi-shim-collector.cpp b/src/ToolBox/superpmi/superpmi-shim-collector/superpmi-shim-collector.cpp
index c8af7a0c26..26f6563262 100644
--- a/src/ToolBox/superpmi/superpmi-shim-collector/superpmi-shim-collector.cpp
+++ b/src/ToolBox/superpmi/superpmi-shim-collector/superpmi-shim-collector.cpp
@@ -107,7 +107,7 @@ void SetLogPathName()
unsigned __int64 randNumber = 0;
const size_t RandNumberLength = sizeof(randNumber) * 2 + 1; // 16 hex digits + null
WCHAR RandNumberString[RandNumberLength];
- PAL_Random(/* bStrong */ FALSE, &randNumber, sizeof(randNumber));
+ PAL_Random(&randNumber, sizeof(randNumber));
swprintf_s(RandNumberString, RandNumberLength, W("%016llX"), randNumber);
#else // !FEATURE_PAL
unsigned int randNumber = 0;
diff --git a/src/ToolBox/superpmi/superpmi-shim-counter/methodcallsummarizer.cpp b/src/ToolBox/superpmi/superpmi-shim-counter/methodcallsummarizer.cpp
index 423007afcb..eb4a56de08 100644
--- a/src/ToolBox/superpmi/superpmi-shim-counter/methodcallsummarizer.cpp
+++ b/src/ToolBox/superpmi/superpmi-shim-counter/methodcallsummarizer.cpp
@@ -49,7 +49,7 @@ MethodCallSummarizer::MethodCallSummarizer(WCHAR* logPath)
dataFileNameLength = MaxAcceptablePathLength;
#ifdef FEATURE_PAL
- PAL_Random(/* bStrong */ FALSE, &randNumber, sizeof(randNumber));
+ PAL_Random(&randNumber, sizeof(randNumber));
#else // !FEATURE_PAL
rand_s(&randNumber);
#endif // !FEATURE_PAL
diff --git a/src/ToolBox/superpmi/superpmi/parallelsuperpmi.cpp b/src/ToolBox/superpmi/superpmi/parallelsuperpmi.cpp
index 596f1b66b1..a773344eb8 100644
--- a/src/ToolBox/superpmi/superpmi/parallelsuperpmi.cpp
+++ b/src/ToolBox/superpmi/superpmi/parallelsuperpmi.cpp
@@ -471,7 +471,7 @@ int doParallelSuperPMI(CommandLine::Options& o)
// Add a random number to the temporary file names to allow multiple parallel SuperPMI to happen at once.
unsigned int randNumber = 0;
#ifdef FEATURE_PAL
- PAL_Random(/* bStrong */ FALSE, &randNumber, sizeof(randNumber));
+ PAL_Random(&randNumber, sizeof(randNumber));
#else // !FEATURE_PAL
rand_s(&randNumber);
#endif // !FEATURE_PAL
diff --git a/src/dlls/mscordac/mscordac_unixexports.src b/src/dlls/mscordac/mscordac_unixexports.src
index 99ed375ce7..77a53d7871 100644
--- a/src/dlls/mscordac/mscordac_unixexports.src
+++ b/src/dlls/mscordac/mscordac_unixexports.src
@@ -29,6 +29,7 @@ PAL_InitializeDLL
PAL_TerminateEx
PAL_IsDebuggerPresent
PAL_ProbeMemory
+PAL_Random
PAL_iswspace
PAL_memcpy
PAL_malloc
@@ -65,7 +66,6 @@ _i64tow_s
memcpy_s
sscanf_s
-CoCreateGuid
CopyFileW
CreateDirectoryW
CreateFileMappingA
diff --git a/src/dlls/mscoree/mscorwks_unixexports.src b/src/dlls/mscoree/mscorwks_unixexports.src
index 0eae53f3f9..a3b00d0761 100644
--- a/src/dlls/mscoree/mscorwks_unixexports.src
+++ b/src/dlls/mscoree/mscorwks_unixexports.src
@@ -22,7 +22,6 @@ GetCLRRuntimeHost
; Win32 API and other PAL functions used by the mscorlib
CloseHandle
-CoCreateGuid
CoTaskMemAlloc
CoTaskMemRealloc
CoTaskMemFree
diff --git a/src/mscorlib/System.Private.CoreLib.csproj b/src/mscorlib/System.Private.CoreLib.csproj
index 1da5ccba25..effac53f59 100644
--- a/src/mscorlib/System.Private.CoreLib.csproj
+++ b/src/mscorlib/System.Private.CoreLib.csproj
@@ -344,7 +344,6 @@
<Compile Include="$(BclSourcesRoot)\System\Enum.cs" />
<Compile Include="$(BclSourcesRoot)\System\Environment.cs" />
<Compile Include="$(BclSourcesRoot)\System\GC.cs" />
- <Compile Include="$(BclSourcesRoot)\System\Guid.CoreCLR.cs" />
<Compile Include="$(BclSourcesRoot)\System\InsufficientMemoryException.cs" />
<Compile Include="$(BclSourcesRoot)\System\Internal.cs" />
<Compile Include="$(BclSourcesRoot)\System\Math.CoreCLR.cs" />
diff --git a/src/mscorlib/shared/Interop/Windows/Ole32/Interop.CoCreateGuid.cs b/src/mscorlib/shared/Interop/Windows/Ole32/Interop.CoCreateGuid.cs
new file mode 100644
index 0000000000..60cfb23ea1
--- /dev/null
+++ b/src/mscorlib/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/mscorlib/shared/System.Private.CoreLib.Shared.projitems b/src/mscorlib/shared/System.Private.CoreLib.Shared.projitems
index 46a161e006..a0c6c0cdce 100644
--- a/src/mscorlib/shared/System.Private.CoreLib.Shared.projitems
+++ b/src/mscorlib/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/mscorlib/shared/System/Guid.Unix.cs b/src/mscorlib/shared/System/Guid.Unix.cs
new file mode 100644
index 0000000000..442e7f8837
--- /dev/null
+++ b/src/mscorlib/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/mscorlib/shared/System/Guid.Windows.cs b/src/mscorlib/shared/System/Guid.Windows.cs
new file mode 100644
index 0000000000..f00fbe45b3
--- /dev/null
+++ b/src/mscorlib/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;
+ }
+ }
+}
+
diff --git a/src/mscorlib/src/Microsoft/Win32/Win32Native.cs b/src/mscorlib/src/Microsoft/Win32/Win32Native.cs
index fbb1b35132..eef34ad8d9 100644
--- a/src/mscorlib/src/Microsoft/Win32/Win32Native.cs
+++ b/src/mscorlib/src/Microsoft/Win32/Win32Native.cs
@@ -380,9 +380,6 @@ namespace Microsoft.Win32
internal static extern uint GetCurrentProcessId();
[DllImport(Interop.Libraries.Ole32)]
- internal extern static int CoCreateGuid(out Guid guid);
-
- [DllImport(Interop.Libraries.Ole32)]
internal static extern IntPtr CoTaskMemAlloc(UIntPtr cb);
[DllImport(Interop.Libraries.Ole32)]
diff --git a/src/mscorlib/src/System/Guid.CoreCLR.cs b/src/mscorlib/src/System/Guid.CoreCLR.cs
deleted file mode 100644
index e3722b8092..0000000000
--- a/src/mscorlib/src/System/Guid.CoreCLR.cs
+++ /dev/null
@@ -1,19 +0,0 @@
-// 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 Microsoft.Win32;
-using System.Runtime.InteropServices;
-
-namespace System
-{
- partial struct Guid
- {
- public static Guid NewGuid()
- {
- Guid guid;
- Marshal.ThrowExceptionForHR(Win32Native.CoCreateGuid(out guid), new IntPtr(-1));
- return guid;
- }
- }
-}
diff --git a/src/pal/inc/pal.h b/src/pal/inc/pal.h
index 18fbdab8bc..462accd4f9 100644
--- a/src/pal/inc/pal.h
+++ b/src/pal/inc/pal.h
@@ -526,10 +526,9 @@ PAL_GetPALDirectoryW(
#endif
PALIMPORT
-BOOL
+VOID
PALAPI
PAL_Random(
- IN BOOL bStrong,
IN OUT LPVOID lpBuffer,
IN DWORD dwLength);
@@ -4876,11 +4875,6 @@ SetThreadIdealProcessorEx(
#define EVENTLOG_AUDIT_SUCCESS 0x0008
#define EVENTLOG_AUDIT_FAILURE 0x0010
-PALIMPORT
-HRESULT
-PALAPI
-CoCreateGuid(OUT GUID * pguid);
-
#if defined FEATURE_PAL_ANSI
#include "palprivate.h"
#endif //FEATURE_PAL_ANSI
diff --git a/src/pal/inc/rt/palrt.h b/src/pal/inc/rt/palrt.h
index af76e0424e..1360a81c43 100644
--- a/src/pal/inc/rt/palrt.h
+++ b/src/pal/inc/rt/palrt.h
@@ -1577,6 +1577,9 @@ EXTERN_C HRESULT PALAPI PAL_CoCreateInstance(REFCLSID rclsid,
// instead of spreading around of if'def FEATURE_PALs for PAL_CoCreateInstance.
#define CoCreateInstance(rclsid, pUnkOuter, dwClsContext, riid, ppv) PAL_CoCreateInstance(rclsid, riid, ppv)
+STDAPI
+CoCreateGuid(OUT GUID * pguid);
+
/************** verrsrc.h ************************************/
/* ----- VS_VERSION.dwFileFlags ----- */
diff --git a/src/pal/src/CMakeLists.txt b/src/pal/src/CMakeLists.txt
index 3ef0dd12de..0285d8878c 100644
--- a/src/pal/src/CMakeLists.txt
+++ b/src/pal/src/CMakeLists.txt
@@ -345,7 +345,6 @@ if(CMAKE_SYSTEM_NAME STREQUAL Linux)
target_link_libraries(coreclrpal
dl
- uuid
)
if(NOT UNWIND_GENERIC STREQUAL UNWIND_GENERIC-NOTFOUND)
diff --git a/src/pal/src/config.h.in b/src/pal/src/config.h.in
index 5643c1b399..584b28aff4 100644
--- a/src/pal/src/config.h.in
+++ b/src/pal/src/config.h.in
@@ -15,8 +15,6 @@
#cmakedefine01 HAVE_SYS_LWP_H
#cmakedefine01 HAVE_LWP_H
#cmakedefine01 HAVE_LIBUNWIND_H
-#cmakedefine01 HAVE_LIBUUID_H
-#cmakedefine01 HAVE_BSD_UUID_H
#cmakedefine01 HAVE_RUNETYPE_H
#cmakedefine01 HAVE_SYS_SYSCTL_H
#cmakedefine01 HAVE_GNU_LIBNAMES_H
diff --git a/src/pal/src/configure.cmake b/src/pal/src/configure.cmake
index 0f105eee94..a6dd6f49c4 100644
--- a/src/pal/src/configure.cmake
+++ b/src/pal/src/configure.cmake
@@ -48,7 +48,6 @@ if(NOT CMAKE_SYSTEM_NAME STREQUAL FreeBSD AND NOT CMAKE_SYSTEM_NAME STREQUAL Net
unset(CMAKE_REQUIRED_FLAGS)
endif()
-check_include_files(uuid/uuid.h HAVE_LIBUUID_H)
check_include_files(sys/sysctl.h HAVE_SYS_SYSCTL_H)
check_include_files(gnu/lib-names.h HAVE_GNU_LIBNAMES_H)
@@ -170,16 +169,6 @@ check_cxx_symbol_exists(_SC_PHYS_PAGES unistd.h HAVE__SC_PHYS_PAGES)
check_cxx_symbol_exists(_SC_AVPHYS_PAGES unistd.h HAVE__SC_AVPHYS_PAGES)
check_cxx_source_runs("
-#include <uuid.h>
-
-int main(void) {
- uuid_t uuid;
- uint32_t status;
- uuid_create(&uuid, &status);
- return 0;
-}" HAVE_BSD_UUID_H)
-
-check_cxx_source_runs("
#include <sys/param.h>
#include <stdlib.h>
@@ -1271,10 +1260,6 @@ if(NOT CLR_CMAKE_PLATFORM_ARCH_ARM AND NOT CLR_CMAKE_PLATFORM_ARCH_ARM64)
endif()
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
- if(NOT HAVE_LIBUUID_H)
- unset(HAVE_LIBUUID_H CACHE)
- message(FATAL_ERROR "Cannot find libuuid. Try installing uuid-dev or the appropriate packages for your platform")
- endif()
set(HAVE_COREFOUNDATION 1)
set(HAVE__NSGETENVIRON 1)
set(DEADLOCK_WHEN_THREAD_IS_SUSPENDED_WHILE_BLOCKED_ON_MUTEX 1)
@@ -1291,10 +1276,6 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
unset(HAVE_LIBUNWIND_H CACHE)
message(FATAL_ERROR "Cannot find libunwind. Try installing libunwind8 and libunwind8-dev (or the appropriate packages for your platform)")
endif()
- if(NOT HAVE_BSD_UUID_H)
- unset(HAVE_BSD_UUID_H CACHE)
- message(FATAL_ERROR "Cannot find uuid.h")
- endif()
set(DEADLOCK_WHEN_THREAD_IS_SUSPENDED_WHILE_BLOCKED_ON_MUTEX 0)
set(PAL_PTRACE "ptrace((cmd), (pid), (caddr_t)(addr), (data))")
set(PAL_PT_ATTACH PT_ATTACH)
@@ -1309,10 +1290,6 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL NetBSD)
unset(HAVE_LIBUNWIND_H CACHE)
message(FATAL_ERROR "Cannot find libunwind. Try installing libunwind8 and libunwind8-dev (or the appropriate packages for your platform)")
endif()
- if(NOT HAVE_BSD_UUID_H)
- unset(HAVE_BSD_UUID_H CACHE)
- message(FATAL_ERROR "Cannot find uuid.h")
- endif()
set(DEADLOCK_WHEN_THREAD_IS_SUSPENDED_WHILE_BLOCKED_ON_MUTEX 0)
set(PAL_PTRACE "ptrace((cmd), (pid), (void*)(addr), (data))")
set(PAL_PT_ATTACH PT_ATTACH)
@@ -1328,10 +1305,6 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL SunOS)
unset(HAVE_LIBUNWIND_H CACHE)
message(FATAL_ERROR "Cannot find libunwind. Try installing libunwind8 and libunwind8-dev (or the appropriate packages for your platform)")
endif()
- if(NOT HAVE_LIBUUID_H)
- unset(HAVE_LIBUUID_H CACHE)
- message(FATAL_ERROR "Cannot find libuuid. Try installing uuid-dev or the appropriate packages for your platform")
- endif()
set(DEADLOCK_WHEN_THREAD_IS_SUSPENDED_WHILE_BLOCKED_ON_MUTEX 0)
set(PAL_PTRACE "ptrace((cmd), (pid), (caddr_t)(addr), (data))")
set(PAL_PT_ATTACH PT_ATTACH)
@@ -1348,10 +1321,6 @@ else() # Anything else is Linux
unset(HAVE_LTTNG_TRACEPOINT_H CACHE)
message(FATAL_ERROR "Cannot find liblttng-ust-dev. Try installing liblttng-ust-dev (or the appropriate packages for your platform)")
endif()
- if(NOT HAVE_LIBUUID_H)
- unset(HAVE_LIBUUID_H CACHE)
- message(FATAL_ERROR "Cannot find libuuid. Try installing uuid-dev or the appropriate packages for your platform")
- endif()
set(DEADLOCK_WHEN_THREAD_IS_SUSPENDED_WHILE_BLOCKED_ON_MUTEX 0)
set(PAL_PTRACE "ptrace((cmd), (pid), (void*)(addr), (data))")
set(PAL_PT_ATTACH PTRACE_ATTACH)
diff --git a/src/pal/src/misc/miscpalapi.cpp b/src/pal/src/misc/miscpalapi.cpp
index 0e1234401e..27ff136a06 100644
--- a/src/pal/src/misc/miscpalapi.cpp
+++ b/src/pal/src/misc/miscpalapi.cpp
@@ -34,12 +34,6 @@ Revision History:
#include <pthread.h>
#include <dlfcn.h>
-#if HAVE_BSD_UUID_H
-#include <uuid.h>
-#elif HAVE_LIBUUID_H
-#include <uuid/uuid.h>
-#endif
-
#include <pal_endian.h>
#ifdef __APPLE__
@@ -48,7 +42,6 @@ Revision History:
SET_DEFAULT_DEBUG_CHANNEL(MISC);
-static const char RANDOM_DEVICE_NAME[] ="/dev/random";
static const char URANDOM_DEVICE_NAME[]="/dev/urandom";
/*++
@@ -235,90 +228,64 @@ PAL_GetPALDirectoryA(
return bRet;
}
-BOOL
+VOID
PALAPI
PAL_Random(
- IN BOOL bStrong,
IN OUT LPVOID lpBuffer,
IN DWORD dwLength)
{
int rand_des = -1;
- BOOL bRet = FALSE;
DWORD i;
- char buf;
long num = 0;
- static BOOL sMissingDevRandom;
static BOOL sMissingDevURandom;
static BOOL sInitializedMRand;
PERF_ENTRY(PAL_Random);
- ENTRY("PAL_Random(bStrong=%d, lpBuffer=%p, dwLength=%d)\n",
- bStrong, lpBuffer, dwLength);
+ ENTRY("PAL_Random(lpBuffer=%p, dwLength=%d)\n", lpBuffer, dwLength);
- i = 0;
-
- if (bStrong == TRUE && i < dwLength && !sMissingDevRandom)
+ if (!sMissingDevURandom)
{
- // request non-blocking access to avoid hangs if the /dev/random is exhausted
- // or just simply broken
- if ((rand_des = PAL__open(RANDOM_DEVICE_NAME, O_RDONLY | O_NONBLOCK)) == -1)
+ do
{
- if (errno == ENOENT)
- {
- sMissingDevRandom = TRUE;
- }
- else
- {
- ASSERT("PAL__open() failed, errno:%d (%s)\n", errno, strerror(errno));
- }
-
- // Back off and try /dev/urandom.
+ rand_des = open("/dev/urandom", O_RDONLY, O_CLOEXEC);
}
- else
- {
- for( ; i < dwLength; i++)
- {
- if (read(rand_des, &buf, 1) < 1)
- {
- // the /dev/random pool has been exhausted. Fall back
- // to /dev/urandom for the remainder of the buffer.
- break;
- }
+ while ((rand_des == -1) && (errno == EINTR));
- *(((BYTE*)lpBuffer) + i) ^= buf;
- }
-
- close(rand_des);
- }
- }
-
- if (i < dwLength && !sMissingDevURandom)
- {
- if ((rand_des = PAL__open(URANDOM_DEVICE_NAME, O_RDONLY)) == -1)
+ if (rand_des == -1)
{
if (errno == ENOENT)
{
- sMissingDevURandom = TRUE;
+ sMissingDevURandom = TRUE;
}
else
{
- ASSERT("PAL__open() failed, errno:%d (%s)\n", errno, strerror(errno));
+ ASSERT("PAL__open() failed, errno:%d (%s)\n", errno, strerror(errno));
}
- // Back off and try mrand48.
+ // Back off and try mrand48.
}
else
{
- for( ; i < dwLength; i++)
+ DWORD offset = 0;
+ do
{
- if (read(rand_des, &buf, 1) < 1)
+ DWORD n = read(rand_des, (BYTE*)lpBuffer + offset , dwLength - offset);
+ if (n == -1)
{
- // Fall back to srand48 for the remainder of the buffer.
+ if (errno == EINTR)
+ {
+ continue;
+ }
+ ASSERT("read() failed, errno:%d (%s)\n", errno, strerror(errno));
+
break;
}
- *(((BYTE*)lpBuffer) + i) ^= buf;
+ offset += n;
}
+ while (offset != dwLength);
+
+ _ASSERTE(offset == dwLength);
close(rand_des);
}
@@ -331,9 +298,9 @@ PAL_Random(
}
// always xor srand48 over the whole buffer to get some randomness
- // in case /dev/random is not really random
+ // in case /dev/urandom is not really random
- for(i = 0; i < dwLength; i++)
+ for (i = 0; i < dwLength; i++)
{
if (i % sizeof(long) == 0) {
num = mrand48();
@@ -343,39 +310,6 @@ PAL_Random(
num >>= 8;
}
- bRet = TRUE;
-
- LOGEXIT("PAL_Random returns %d\n", bRet);
+ LOGEXIT("PAL_Random\n");
PERF_EXIT(PAL_Random);
- return bRet;
-}
-
-HRESULT
-PALAPI
-CoCreateGuid(OUT GUID * pguid)
-{
-#if HAVE_BSD_UUID_H
- uuid_t uuid;
- uint32_t status;
- uuid_create(&uuid, &status);
- if (status != uuid_s_ok)
- {
- ASSERT("Unexpected uuid_create failure (status=%u)\n", status);
- PROCAbort();
- }
-
- // Encode the uuid with little endian.
- uuid_enc_le(pguid, &uuid);
-#elif HAVE_LIBUUID_H
- uuid_generate_random(*(uuid_t*)pguid);
-
- // Change the byte order of the Data1, 2 and 3, since the uuid_generate_random
- // generates them with big endian while GUIDS need to have them in little endian.
- pguid->Data1 = SWAP32(pguid->Data1);
- pguid->Data2 = SWAP16(pguid->Data2);
- pguid->Data3 = SWAP16(pguid->Data3);
-#else
- #error Don't know how to generate UUID on this platform
-#endif
- return 0;
}
diff --git a/src/palrt/guid.cpp b/src/palrt/guid.cpp
index bd782c8502..14cd3bb490 100644
--- a/src/palrt/guid.cpp
+++ b/src/palrt/guid.cpp
@@ -24,3 +24,25 @@ DEFINE_GUID(IID_IClassFactory, 0x00000001, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x0
// objidl.idl
DEFINE_GUID(IID_ISequentialStream, 0x0c733a30, 0x2a1c, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
DEFINE_GUID(IID_IStream, 0x0000000c, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46);
+
+// Create a random guid based on the https://www.ietf.org/rfc/rfc4122.txt
+STDAPI
+CoCreateGuid(OUT GUID * pguid)
+{
+ PAL_Random(pguid, sizeof(GUID));
+
+ static const USHORT VersionMask = 0xF000;
+ static const USHORT RandomGuidVersion = 0x4000;
+
+ static const BYTE ClockSeqHiAndReservedMask = 0xC0;
+ static const BYTE ClockSeqHiAndReservedValue = 0x80;
+
+ // Modify bits indicating the type of the GUID
+
+ // time_hi_and_version
+ pguid->Data3 = (pguid->Data3 & ~VersionMask) | RandomGuidVersion;
+ // clock_seq_hi_and_reserved
+ pguid->Data4[0] = (pguid->Data4[0] & ~ClockSeqHiAndReservedMask) | ClockSeqHiAndReservedValue;
+
+ return S_OK;
+}
diff --git a/src/vm/comutilnative.cpp b/src/vm/comutilnative.cpp
index 3b30d4ce87..8fc326e814 100644
--- a/src/vm/comutilnative.cpp
+++ b/src/vm/comutilnative.cpp
@@ -2337,7 +2337,7 @@ PCBYTE COMNlsHashProvider::GetEntropy()
AllocMemHolder<BYTE> pNewEntropy(GetAppDomain()->GetLowFrequencyHeap()->AllocMem(S_SIZE_T(sizeof(SYMCRYPT_MARVIN32_SEED_SIZE))));
#ifdef FEATURE_PAL
- PAL_Random(TRUE, pNewEntropy, SYMCRYPT_MARVIN32_SEED_SIZE);
+ PAL_Random(pNewEntropy, SYMCRYPT_MARVIN32_SEED_SIZE);
#else
HCRYPTPROV hCryptProv;
WszCryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);