diff options
author | Jan Vorlicek <janvorli@microsoft.com> | 2018-03-06 02:21:09 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-06 02:21:09 +0100 |
commit | 76245a33daf1e4a8dc724e9b7fd712bd41a7aee0 (patch) | |
tree | 5067dae50136e8caca443e2d749814e05f653e3f /src/pal/src/misc | |
parent | a2628fc507df0c011383dd0c275bfb0d51bcd060 (diff) | |
download | coreclr-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/pal/src/misc')
-rw-r--r-- | src/pal/src/misc/miscpalapi.cpp | 120 |
1 files changed, 27 insertions, 93 deletions
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; } |