Title: [288116] trunk
Revision
288116
Author
[email protected]
Date
2022-01-18 08:16:36 -0800 (Tue, 18 Jan 2022)

Log Message

Make ServiceWorkerClient.id a UUID instead of a string derived from a ScriptExecutionContextIdentifier
https://bugs.webkit.org/show_bug.cgi?id=235070
<rdar://problem/87673540>

Reviewed by Darin Adler and Chris Dumez.
Source/WebCore:

Covered by existing and beefed-up API tests.

* platform/ScriptExecutionContextIdentifier.h:
Remove unneeded const & and && constructors and use by value constructor instead.
Add computeHash direct specialization to remove UUID Hasher specialization.

Source/WTF:

* wtf/UUID.cpp:
* wtf/UUID.h:
Use uint64_t logic to generate UUIDs and remove some reinterpret_cast.
Remove the Hasher add since we directly use UUID hash in WebCore.
Simplify parsing logic by using more parseIntegers and bit logic.
Also make sure we are not able to create empty/deleted UUID value from strings.

Tools:

* TestWebKitAPI/Tests/WTF/UUID.cpp:
Improve test coverage.

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (288115 => 288116)


--- trunk/Source/WTF/ChangeLog	2022-01-18 15:25:32 UTC (rev 288115)
+++ trunk/Source/WTF/ChangeLog	2022-01-18 16:16:36 UTC (rev 288116)
@@ -1,3 +1,18 @@
+2022-01-18  Youenn Fablet  <[email protected]>
+
+        Make ServiceWorkerClient.id a UUID instead of a string derived from a ScriptExecutionContextIdentifier
+        https://bugs.webkit.org/show_bug.cgi?id=235070
+        <rdar://problem/87673540>
+
+        Reviewed by Darin Adler and Chris Dumez.
+
+        * wtf/UUID.cpp:
+        * wtf/UUID.h:
+        Use uint64_t logic to generate UUIDs and remove some reinterpret_cast.
+        Remove the Hasher add since we directly use UUID hash in WebCore.
+        Simplify parsing logic by using more parseIntegers and bit logic.
+        Also make sure we are not able to create empty/deleted UUID value from strings.
+
 2022-01-17  Youenn Fablet  <[email protected]>
 
         Make ServiceWorkerClient.id a UUID instead of a string derived from a ScriptExecutionContextIdentifier

Modified: trunk/Source/WTF/wtf/UUID.cpp (288115 => 288116)


--- trunk/Source/WTF/wtf/UUID.cpp	2022-01-18 15:25:32 UTC (rev 288115)
+++ trunk/Source/WTF/wtf/UUID.cpp	2022-01-18 16:16:36 UTC (rev 288116)
@@ -52,9 +52,11 @@
         cryptographicallyRandomValues(data, 16);
     } while (m_data == emptyValue || m_data == deletedValue);
 
-    // We sanitize the value so as not loosing any data when going to Version 4 UUIDs.
-    data[5] &= 0x0f;
-    data[11] &= 0xcf;
+    // We sanitize the value to not loose any information when serializing as Version 4 UUID.
+    auto high = static_cast<uint64_t>((m_data >> 64) & 0xffffffffffff0fff);
+    auto low = static_cast<uint64_t>(m_data & 0x3fffffffffffffff);
+
+    m_data = (static_cast<UInt128>(high) << 64) | low;
 }
 
 unsigned UUID::hash() const
@@ -64,21 +66,20 @@
 
 String UUID::toString() const
 {
-    auto* randomData = reinterpret_cast<const unsigned*>(&m_data);
+    auto high = static_cast<uint64_t>(m_data >> 64);
+    auto low = static_cast<uint64_t>(m_data & 0x3fffffffffffffff);
 
     // Format as Version 4 UUID.
     return makeString(
-        hex(randomData[0], 8, Lowercase),
+        hex(high >> 32, 8, Lowercase),
         '-',
-        hex(randomData[1] >> 16, 4, Lowercase),
+        hex((high >> 16) & 0xffff, 4, Lowercase),
         "-4",
-        hex(randomData[1] & 0x00000fff, 3, Lowercase),
+        hex(high & 0xfff, 3, Lowercase),
         '-',
-        hex((randomData[2] >> 30) | 0x8, 1, Lowercase),
-        hex((randomData[2] >> 16) & 0x00000fff, 3, Lowercase),
+        hex((low >> 48) | 0x8000, 4, Lowercase),
         '-',
-        hex(randomData[2] & 0x0000ffff, 4, Lowercase),
-        hex(randomData[3], 8, Lowercase)
+        hex(low & 0xffffffffffff, 12, Lowercase)
     );
 }
 
@@ -91,49 +92,43 @@
     if (value[8] != '-' || value[13] != '-'  || value[14] != '4' || value[18] != '-' || value[23] != '-')
         return { };
 
-    unsigned y = value[19];
-    if (y == '8' || y == '9')
-        y = y - '8';
-    else if (y == 'a' || y == 'A')
-        y = 2;
-    else if (y == 'b' || y == 'B')
-        y = 3;
-    else
+    // parseInteger may accept integers starting with +, let's check this beforehand.
+    if (value[0] == '+' || value[9] == '+'  || value[19] == '+' || value[24] == '+')
         return { };
 
-    auto firstValue = parseInteger<unsigned>(value.substring(0, 8), 16);
+    auto firstValue = parseInteger<uint64_t>(value.substring(0, 8), 16);
     if (!firstValue)
         return { };
 
-    auto secondValue = parseInteger<unsigned>(value.substring(9, 4), 16);
+    auto secondValue = parseInteger<uint64_t>(value.substring(9, 4), 16);
     if (!secondValue)
         return { };
 
-    auto thirdValue = parseInteger<unsigned>(value.substring(15, 3), 16);
+    auto thirdValue = parseInteger<uint64_t>(value.substring(15, 3), 16);
     if (!thirdValue)
         return { };
 
-    auto fourthValue = parseInteger<unsigned>(value.substring(20, 3), 16);
+    auto fourthValue = parseInteger<uint64_t>(value.substring(19, 4), 16);
     if (!fourthValue)
         return { };
 
-    auto fifthValue = parseInteger<unsigned>(value.substring(24, 4), 16);
-    if (!fifthValue)
+    // Fourth value starts with 'y', it must be above 0x8000 and below 0xBFFFF.
+    if ((*fourthValue & 0xc000) != 0x8000)
         return { };
+    fourthValue = *fourthValue & 0x3fff;
 
-    auto sixthValue = parseInteger<unsigned>(value.substring(28, 8), 16);
-    if (!sixthValue)
+    auto fifthValue = parseInteger<uint64_t>(value.substring(24, 12), 16);
+    if (!fifthValue)
         return { };
 
-    UInt128 uuidValue;
+    uint64_t high = (*firstValue << 32) | (*secondValue << 16) | *thirdValue;
+    uint64_t low = (*fourthValue << 48) | *fifthValue;
 
-    auto* data = ""
-    data[0] = *firstValue;
-    data[1] = (*secondValue << 16) | *thirdValue;
-    data[2] = ((*fourthValue << 16) | *fifthValue) + (y << 30);
-    data[3] = *sixthValue;
+    auto result = (static_cast<UInt128>(high) << 64) | low;
+    if (result == emptyValue || result == deletedValue)
+        return { };
 
-    return UUID(WTFMove(uuidValue));
+    return UUID(result);
 }
 
 String createCanonicalUUIDString()

Modified: trunk/Source/WTF/wtf/UUID.h (288115 => 288116)


--- trunk/Source/WTF/wtf/UUID.h	2022-01-18 15:25:32 UTC (rev 288115)
+++ trunk/Source/WTF/wtf/UUID.h	2022-01-18 16:16:36 UTC (rev 288116)
@@ -126,11 +126,6 @@
     return UUID { (static_cast<UInt128>(*high) << 64) | *low };
 }
 
-inline void add(Hasher& hasher, const UUID& uuid)
-{
-    add(hasher, uuid.hash());
-}
-
 // Creates a UUID that consists of 32 hexadecimal digits and returns its canonical form.
 // The canonical form is displayed in 5 groups separated by hyphens, in the form 8-4-4-4-12 for a total of 36 characters.
 // The hexadecimal values "a" through "f" are output as lower case characters.

Modified: trunk/Source/WebCore/ChangeLog (288115 => 288116)


--- trunk/Source/WebCore/ChangeLog	2022-01-18 15:25:32 UTC (rev 288115)
+++ trunk/Source/WebCore/ChangeLog	2022-01-18 16:16:36 UTC (rev 288116)
@@ -1,3 +1,17 @@
+2022-01-18  Youenn Fablet  <[email protected]>
+
+        Make ServiceWorkerClient.id a UUID instead of a string derived from a ScriptExecutionContextIdentifier
+        https://bugs.webkit.org/show_bug.cgi?id=235070
+        <rdar://problem/87673540>
+
+        Reviewed by Darin Adler and Chris Dumez.
+
+        Covered by existing and beefed-up API tests.
+
+        * platform/ScriptExecutionContextIdentifier.h:
+        Remove unneeded const & and && constructors and use by value constructor instead.
+        Add computeHash direct specialization to remove UUID Hasher specialization.
+
 2022-01-18  Oriol Brufau  <[email protected]>
 
         [css-logical] Simplify logical property groups

Modified: trunk/Source/WebCore/platform/ScriptExecutionContextIdentifier.h (288115 => 288116)


--- trunk/Source/WebCore/platform/ScriptExecutionContextIdentifier.h	2022-01-18 15:25:32 UTC (rev 288115)
+++ trunk/Source/WebCore/platform/ScriptExecutionContextIdentifier.h	2022-01-18 16:16:36 UTC (rev 288116)
@@ -37,22 +37,16 @@
     static ProcessQualified generate() { return { UUID::create(), Process::identifier() }; }
 
     ProcessQualified()
-        : m_object(WTF::UInt128(0))
+        : m_object(UUID::emptyValue)
     {
     }
 
-    ProcessQualified(UUID&& object, ProcessIdentifier processIdentifier)
+    ProcessQualified(UUID object, ProcessIdentifier processIdentifier)
         : m_object(WTFMove(object))
         , m_processIdentifier(processIdentifier)
     {
     }
 
-    ProcessQualified(const UUID& object, ProcessIdentifier processIdentifier)
-        : m_object(object)
-        , m_processIdentifier(processIdentifier)
-    {
-    }
-
     ProcessQualified(WTF::HashTableDeletedValueType)
         : m_object(WTF::HashTableDeletedValue)
         , m_processIdentifier(WTF::HashTableDeletedValue)
@@ -103,3 +97,13 @@
 using ScriptExecutionContextIdentifier = ProcessQualified<UUID>;
 
 }
+
+namespace WTF {
+
+template<>
+inline uint32_t computeHash(const WebCore::ScriptExecutionContextIdentifier& identifier)
+{
+    return identifier.object().hash();
+}
+
+}

Modified: trunk/Tools/ChangeLog (288115 => 288116)


--- trunk/Tools/ChangeLog	2022-01-18 15:25:32 UTC (rev 288115)
+++ trunk/Tools/ChangeLog	2022-01-18 16:16:36 UTC (rev 288116)
@@ -1,3 +1,13 @@
+2022-01-18  Youenn Fablet  <[email protected]>
+
+        Make ServiceWorkerClient.id a UUID instead of a string derived from a ScriptExecutionContextIdentifier
+        https://bugs.webkit.org/show_bug.cgi?id=235070
+        <rdar://problem/87673540>
+
+        Reviewed by Darin Adler and Chris Dumez.
+        * TestWebKitAPI/Tests/WTF/UUID.cpp:
+        Improve test coverage.
+
 2022-01-17  Wenson Hsieh  <[email protected]>
 
         ImageAnalysisQueue should analyze image elements that are loaded after the call to enqueueAllImages()

Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/UUID.cpp (288115 => 288116)


--- trunk/Tools/TestWebKitAPI/Tests/WTF/UUID.cpp	2022-01-18 15:25:32 UTC (rev 288115)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/UUID.cpp	2022-01-18 16:16:36 UTC (rev 288116)
@@ -32,25 +32,53 @@
     EXPECT_EQ(bootSessionUUIDString(), bootSessionUUIDString());
 }
 
+static String parseAndStringifyUUID(const String& value)
+{
+    auto uuid = UUID::parse(value);
+    if (!uuid)
+        return { };
+    return uuid->toString();
+}
+
 TEST(WTF, TestUUIDParsing)
 {
-    auto created  = UUID::create();
-    auto value = UUID::parse(created.toString());
-    EXPECT_TRUE(!!value);
-    EXPECT_EQ(created, *value);
-
     // xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
-    value = UUID::parse("12345678-9abc-5de0-89AB-0123456789ab");
-    EXPECT_FALSE(!!value);
 
-    value = UUID::parse("12345678-9abc-4dea-79AB-0123456789ab");
-    EXPECT_FALSE(!!value);
+    EXPECT_FALSE(!!UUID::parse("12345678-9abc-5de0-89AB-0123456789ab"));
+    EXPECT_FALSE(!!UUID::parse("12345678-9abc-4dea-79AB-0123456789ab"));
+    EXPECT_FALSE(!!UUID::parse("12345678-9abc-4de0-7fff-0123456789ab"));
+    EXPECT_FALSE(!!UUID::parse("12345678-9abc-4de0-c0000-0123456789ab"));
 
-    value = UUID::parse("12345678-9abc-4de0-89ab-0123456789ab");
-    EXPECT_TRUE(!!value);
-    EXPECT_TRUE(value->toString() == "12345678-9abc-4de0-89ab-0123456789ab");
+    EXPECT_FALSE(!!UUID::parse("+ef944c1-5cb8-48aa-Ad12-C5f823f005c3"));
+    EXPECT_FALSE(!!UUID::parse("6ef944c1-+cb8-48aa-Ad12-C5f823f005c3"));
+    EXPECT_FALSE(!!UUID::parse("6ef944c1-5cb8-+8aa-Ad12-C5f823f005c3"));
+    EXPECT_FALSE(!!UUID::parse("6ef944c1-5cb8-48aa-+d12-C5f823f005c3"));
+    EXPECT_FALSE(!!UUID::parse("6ef944c1-5cb8-48aa-Ad12-+5f823f005c3"));
 
-    value = UUID::parse("6ef944c1-5cb8-48aa-Ad12-C5f823f005c3");
-    EXPECT_TRUE(!!value);
-    EXPECT_TRUE(value->toString() == "6ef944c1-5cb8-48aa-ad12-c5f823f005c3");
+    EXPECT_FALSE(!!UUID::parse("00000000-0000-4000-8000-000000000000"));
+    EXPECT_FALSE(!!UUID::parse("00000000-0000-4000-8000-000000000001"));
+
+    for (size_t cptr = 0; cptr < 100; ++cptr) {
+        auto createdUUID = UUID::create();
+        auto createdString = createdUUID.toString();
+        EXPECT_EQ(createdString.length(), 36u);
+        EXPECT_EQ(createdString[14], '4');
+        EXPECT_TRUE(createdString[19] == '8' || createdString[19] == '9' || createdString[19] == 'a' || createdString[19] == 'b');
+
+        auto uuid = UUID::parse(createdString);
+        EXPECT_TRUE(!!uuid);
+        EXPECT_EQ(*uuid, createdUUID);
+    }
+
+    String testNormal = "12345678-9abc-4de0-89ab-0123456789ab"_s;
+    EXPECT_EQ(parseAndStringifyUUID(testNormal), testNormal);
+
+    String test8000 = "12345678-9abc-4de0-8000-0123456789ab"_s;
+    EXPECT_EQ(parseAndStringifyUUID(test8000), test8000);
+
+    String testBfff = "12345678-9abc-4de0-Bfff-0123456789ab"_s;
+    EXPECT_EQ(parseAndStringifyUUID(testBfff), testBfff.convertToASCIILowercase());
+
+    String testAd12 = "6ef944c1-5cb8-48aa-Ad12-C5f823f005c3"_s;
+    EXPECT_EQ(parseAndStringifyUUID(testAd12), testAd12.convertToASCIILowercase());
 }
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to