[PATCH] D40623: Correctly set reserved bits for UUID version 4.

Rui Ueyama via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 29 14:54:11 PST 2017


ruiu created this revision.
Herald added subscribers: hiraditya, emaste.

Correctly set reserved bits for UUID version 4.


https://reviews.llvm.org/D40623

Files:
  lld/ELF/SyntheticSections.cpp
  llvm/include/llvm/Support/RandomNumberGenerator.h
  llvm/lib/Support/RandomNumberGenerator.cpp


Index: llvm/lib/Support/RandomNumberGenerator.cpp
===================================================================
--- llvm/lib/Support/RandomNumberGenerator.cpp
+++ llvm/lib/Support/RandomNumberGenerator.cpp
@@ -89,3 +89,15 @@
   return std::error_code(errno, std::system_category());
 #endif
 }
+
+// UUID v4 is a 122-bit random number in the form of
+// RRRRRRRR-RRRR-4RRR-xRRR-RRRRRRRRRRRR, where R is a random byte and
+// x's most siginificant two bits are 0b10.
+std::error_code llvm::getUuidV4(void *Buffer) {
+  if (auto EC = getRandomBytes(Buffer, 16))
+    return EC;
+  char *Buf = (char *)Buffer;
+  Buf[7] = 0x40 | (Buf[7] & 0xf);
+  Buf[9] = 0x80 | (Buf[9] & 0x3f);
+  return std::error_code();
+}
Index: llvm/include/llvm/Support/RandomNumberGenerator.h
===================================================================
--- llvm/include/llvm/Support/RandomNumberGenerator.h
+++ llvm/include/llvm/Support/RandomNumberGenerator.h
@@ -65,6 +65,9 @@
 
 // Get random vector of specified size
 std::error_code getRandomBytes(void *Buffer, size_t Size);
+
+// Get UUID v4.
+std::error_code getUuidV4(void *Buffer);
 }
 
 #endif
Index: lld/ELF/SyntheticSections.cpp
===================================================================
--- lld/ELF/SyntheticSections.cpp
+++ lld/ELF/SyntheticSections.cpp
@@ -368,7 +368,7 @@
     });
     break;
   case BuildIdKind::Uuid:
-    if (auto EC = getRandomBytes(HashBuf, HashSize))
+    if (auto EC = getUuidV4(HashBuf))
       error("entropy source failure: " + EC.message());
     break;
   case BuildIdKind::Hexstring:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D40623.124826.patch
Type: text/x-patch
Size: 1578 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20171129/cf45aa73/attachment.bin>


More information about the llvm-commits mailing list