Title: [286492] trunk
Revision
286492
Author
[email protected]
Date
2021-12-03 08:50:58 -0800 (Fri, 03 Dec 2021)

Log Message

Add room for more bytecode in WKContentRuleList file format
https://bugs.webkit.org/show_bug.cgi?id=233780

Reviewed by Tim Hatcher.

Source/WebCore:

For rdar://72203352 we will need more bytecode.  This adds room for it without putting anything there yet.
As long as we are updating the version number and forcing a recompile, I'm making the string serialization
more like the other action serializations to be able to simplify the interpreter later.  I'm also shifting
around a few bits in the bytecode to be more organized and reasonable.

* contentextensions/CompiledContentExtension.h:
* contentextensions/ContentExtensionStringSerialization.cpp:
(WebCore::ContentExtensions::deserializeString):
(WebCore::ContentExtensions::serializeString):
(WebCore::ContentExtensions::stringSerializedLength):
* contentextensions/DFABytecode.h:
(WebCore::ContentExtensions::smallestPossibleJumpSize): Deleted.
* contentextensions/DFABytecodeCompiler.cpp:
(WebCore::ContentExtensions::smallestPossibleJumpSize):

Source/WebKit:

* Shared/WebCompiledContentRuleList.cpp:
(WebKit::WebCompiledContentRuleList::frameURLFiltersBytecode const):
* Shared/WebCompiledContentRuleList.h:
* Shared/WebCompiledContentRuleListData.cpp:
(WebKit::WebCompiledContentRuleListData::encode const):
(WebKit::WebCompiledContentRuleListData::decode):
* Shared/WebCompiledContentRuleListData.h:
(WebKit::WebCompiledContentRuleListData::WebCompiledContentRuleListData):
* UIProcess/API/APIContentRuleListStore.cpp:
(API::ContentRuleListMetaData::fileSize const):
(API::encodeContentRuleListMetaData):
(API::decodeContentRuleListMetaData):
(API::compiledToFile):
(API::createExtension):
(API::getContentRuleListSourceFromMappedFile):
* UIProcess/API/APIContentRuleListStore.h:

Tools:

* TestWebKitAPI/Tests/WebCore/ContentExtensions.cpp:

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (286491 => 286492)


--- trunk/Source/WebCore/ChangeLog	2021-12-03 16:35:55 UTC (rev 286491)
+++ trunk/Source/WebCore/ChangeLog	2021-12-03 16:50:58 UTC (rev 286492)
@@ -1,3 +1,25 @@
+2021-12-03  Alex Christensen  <[email protected]>
+
+        Add room for more bytecode in WKContentRuleList file format
+        https://bugs.webkit.org/show_bug.cgi?id=233780
+
+        Reviewed by Tim Hatcher.
+
+        For rdar://72203352 we will need more bytecode.  This adds room for it without putting anything there yet.
+        As long as we are updating the version number and forcing a recompile, I'm making the string serialization
+        more like the other action serializations to be able to simplify the interpreter later.  I'm also shifting
+        around a few bits in the bytecode to be more organized and reasonable.
+
+        * contentextensions/CompiledContentExtension.h:
+        * contentextensions/ContentExtensionStringSerialization.cpp:
+        (WebCore::ContentExtensions::deserializeString):
+        (WebCore::ContentExtensions::serializeString):
+        (WebCore::ContentExtensions::stringSerializedLength):
+        * contentextensions/DFABytecode.h:
+        (WebCore::ContentExtensions::smallestPossibleJumpSize): Deleted.
+        * contentextensions/DFABytecodeCompiler.cpp:
+        (WebCore::ContentExtensions::smallestPossibleJumpSize):
+
 2021-12-03  Eric Carlson  <[email protected]>
 
         DisplayCaptureSource doesn't initialize base class correctly

Modified: trunk/Source/WebCore/contentextensions/CompiledContentExtension.h (286491 => 286492)


--- trunk/Source/WebCore/contentextensions/CompiledContentExtension.h	2021-12-03 16:35:55 UTC (rev 286491)
+++ trunk/Source/WebCore/contentextensions/CompiledContentExtension.h	2021-12-03 16:50:58 UTC (rev 286492)
@@ -41,6 +41,7 @@
     virtual Span<const uint8_t> filtersWithoutConditionsBytecode() const = 0;
     virtual Span<const uint8_t> filtersWithConditionsBytecode() const = 0;
     virtual Span<const uint8_t> topURLFiltersBytecode() const = 0;
+    virtual Span<const uint8_t> frameURLFiltersBytecode() const = 0;
     virtual Span<const uint8_t> serializedActions() const = 0;
     virtual bool conditionsApplyOnlyToDomain() const = 0;
 };

Modified: trunk/Source/WebCore/contentextensions/ContentExtensionStringSerialization.cpp (286491 => 286492)


--- trunk/Source/WebCore/contentextensions/ContentExtensionStringSerialization.cpp	2021-12-03 16:35:55 UTC (rev 286491)
+++ trunk/Source/WebCore/contentextensions/ContentExtensionStringSerialization.cpp	2021-12-03 16:50:58 UTC (rev 286492)
@@ -32,51 +32,22 @@
 
 String deserializeString(Span<const uint8_t> span)
 {
-    const auto* actions = span.data();
-    const auto actionsLength = span.size();
-    auto prefixLength = sizeof(uint32_t) + sizeof(bool);
-    auto stringStartIndex = prefixLength;
-    RELEASE_ASSERT(actionsLength >= stringStartIndex);
-    uint32_t stringLength = *reinterpret_cast<const uint32_t*>(actions);
-    bool wideCharacters = actions[sizeof(uint32_t)];
-
-    if (wideCharacters) {
-        RELEASE_ASSERT(actionsLength >= stringStartIndex + stringLength * sizeof(UChar));
-        return String(reinterpret_cast<const UChar*>(&actions[stringStartIndex]), stringLength);
-    }
-    RELEASE_ASSERT(actionsLength >= stringStartIndex + stringLength * sizeof(LChar));
-    return String(reinterpret_cast<const LChar*>(&actions[stringStartIndex]), stringLength);
+    auto serializedLength = *reinterpret_cast<const uint32_t*>(span.data());
+    return String::fromUTF8(span.data() + sizeof(uint32_t), serializedLength - sizeof(uint32_t));
 }
 
 void serializeString(Vector<uint8_t>& actions, const String& string)
 {
-    // Append Selector length (4 bytes).
-    uint32_t stringLength = string.length();
-    actions.grow(actions.size() + sizeof(uint32_t));
-    *reinterpret_cast<uint32_t*>(&actions[actions.size() - sizeof(uint32_t)]) = stringLength;
-    bool wideCharacters = !string.is8Bit();
-    actions.append(wideCharacters);
-    // Append Selector.
-    if (wideCharacters) {
-        uint32_t startIndex = actions.size();
-        actions.grow(actions.size() + sizeof(UChar) * stringLength);
-        for (uint32_t i = 0; i < stringLength; ++i)
-            *reinterpret_cast<UChar*>(&actions[startIndex + i * sizeof(UChar)]) = string[i];
-    } else {
-        for (uint32_t i = 0; i < stringLength; ++i)
-            actions.append(string[i]);
-    }
+    auto utf8 = string.utf8();
+    uint32_t serializedLength = sizeof(uint32_t) + utf8.length();
+    actions.reserveCapacity(actions.size() + serializedLength);
+    actions.uncheckedAppend(Span<const uint8_t> { reinterpret_cast<const uint8_t*>(&serializedLength), sizeof(serializedLength) });
+    actions.uncheckedAppend(Span<const uint8_t> { reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length() });
 }
 
 size_t stringSerializedLength(Span<const uint8_t> span)
 {
-    constexpr auto prefixLength = sizeof(uint32_t) + sizeof(bool);
-    RELEASE_ASSERT(span.size() >= prefixLength);
-    auto stringLength = *reinterpret_cast<const uint32_t*>(span.data());
-    bool wideCharacters = span[sizeof(uint32_t)];
-    if (wideCharacters)
-        return prefixLength + stringLength * sizeof(UChar);
-    return prefixLength + stringLength * sizeof(LChar);
+    return *reinterpret_cast<const uint32_t*>(span.data());
 }
 
 } // namespace WebCore::ContentExtensions

Modified: trunk/Source/WebCore/contentextensions/DFABytecode.h (286491 => 286492)


--- trunk/Source/WebCore/contentextensions/DFABytecode.h	2021-12-03 16:35:55 UTC (rev 286491)
+++ trunk/Source/WebCore/contentextensions/DFABytecode.h	2021-12-03 16:50:58 UTC (rev 286492)
@@ -78,7 +78,7 @@
 
 // The last four bits contain the instruction type.
 static constexpr uint8_t DFABytecodeInstructionMask = 0x0F;
-static constexpr uint8_t DFABytecodeJumpSizeMask = 0xF0;
+static constexpr uint8_t DFABytecodeJumpSizeMask = 0x30;
 static constexpr uint8_t DFABytecodeFlagsSizeMask = 0x30;
 static constexpr uint8_t DFABytecodeActionSizeMask = 0xC0;
 
@@ -87,23 +87,23 @@
 
 // DFABytecodeFlagsSize and DFABytecodeActionSize are stored in the top four bits of the DFABytecodeInstructions that have flags and actions.
 enum class DFABytecodeFlagsSize : uint8_t {
-    UInt8 = 0x10,
-    UInt16 = 0x00, // Needs to be zero to be binary compatible with bytecode compiled with fixed-size flags.
+    UInt8 = 0x00,
+    UInt16 = 0x10,
     UInt24 = 0x20,
 };
 enum class DFABytecodeActionSize : uint8_t {
-    UInt8 = 0x40,
-    UInt16 = 0x80,
-    UInt24 = 0xC0,
-    UInt32 = 0x00, // Needs to be zero to be binary compatible with bytecode compiled with fixed-size actions.
+    UInt8 = 0x00,
+    UInt16 = 0x40,
+    UInt24 = 0x80,
+    UInt32 = 0xC0,
 };
 
 // A DFABytecodeJumpSize is stored in the top four bits of the DFABytecodeInstructions that have a jump.
 enum class DFABytecodeJumpSize : uint8_t {
-    Int8 = 0x10,
-    Int16 = 0x20,
-    Int24 = 0x30,
-    Int32 = 0x40,
+    Int8 = 0x00,
+    Int16 = 0x10,
+    Int24 = 0x20,
+    Int32 = 0x30,
 };
 static constexpr int32_t UInt24Max = (1 << 24) - 1;
 static constexpr int32_t Int24Max = (1 << 23) - 1;
@@ -111,17 +111,6 @@
 static constexpr size_t Int24Size = 3;
 static constexpr size_t UInt24Size = 3;
 
-static inline DFABytecodeJumpSize smallestPossibleJumpSize(int32_t longestPossibleJump)
-{
-    if (longestPossibleJump <= std::numeric_limits<int8_t>::max() && longestPossibleJump >= std::numeric_limits<int8_t>::min())
-        return DFABytecodeJumpSize::Int8;
-    if (longestPossibleJump <= std::numeric_limits<int16_t>::max() && longestPossibleJump >= std::numeric_limits<int16_t>::min())
-        return DFABytecodeJumpSize::Int16;
-    if (longestPossibleJump <= Int24Max && longestPossibleJump >= Int24Min)
-        return DFABytecodeJumpSize::Int24;
-    return DFABytecodeJumpSize::Int32;
-}
-
 } // namespace WebCore::ContentExtensions
 
 #endif // ENABLE(CONTENT_EXTENSIONS)

Modified: trunk/Source/WebCore/contentextensions/DFABytecodeCompiler.cpp (286491 => 286492)


--- trunk/Source/WebCore/contentextensions/DFABytecodeCompiler.cpp	2021-12-03 16:35:55 UTC (rev 286491)
+++ trunk/Source/WebCore/contentextensions/DFABytecodeCompiler.cpp	2021-12-03 16:50:58 UTC (rev 286492)
@@ -151,7 +151,18 @@
     ASSERT(m_nodeStartOffsets[destinationNodeIndex] <= instructionLocation);
     return m_nodeStartOffsets[destinationNodeIndex] - instructionLocation;
 }
-    
+
+static DFABytecodeJumpSize smallestPossibleJumpSize(int32_t longestPossibleJump)
+{
+    if (longestPossibleJump <= std::numeric_limits<int8_t>::max() && longestPossibleJump >= std::numeric_limits<int8_t>::min())
+        return DFABytecodeJumpSize::Int8;
+    if (longestPossibleJump <= std::numeric_limits<int16_t>::max() && longestPossibleJump >= std::numeric_limits<int16_t>::min())
+        return DFABytecodeJumpSize::Int16;
+    if (longestPossibleJump <= Int24Max && longestPossibleJump >= Int24Min)
+        return DFABytecodeJumpSize::Int24;
+    return DFABytecodeJumpSize::Int32;
+}
+
 void DFABytecodeCompiler::emitJump(uint32_t sourceNodeIndex, uint32_t destinationNodeIndex)
 {
     uint32_t instructionLocation = m_bytecode.size();

Modified: trunk/Source/WebKit/ChangeLog (286491 => 286492)


--- trunk/Source/WebKit/ChangeLog	2021-12-03 16:35:55 UTC (rev 286491)
+++ trunk/Source/WebKit/ChangeLog	2021-12-03 16:50:58 UTC (rev 286492)
@@ -1,3 +1,27 @@
+2021-12-03  Alex Christensen  <[email protected]>
+
+        Add room for more bytecode in WKContentRuleList file format
+        https://bugs.webkit.org/show_bug.cgi?id=233780
+
+        Reviewed by Tim Hatcher.
+
+        * Shared/WebCompiledContentRuleList.cpp:
+        (WebKit::WebCompiledContentRuleList::frameURLFiltersBytecode const):
+        * Shared/WebCompiledContentRuleList.h:
+        * Shared/WebCompiledContentRuleListData.cpp:
+        (WebKit::WebCompiledContentRuleListData::encode const):
+        (WebKit::WebCompiledContentRuleListData::decode):
+        * Shared/WebCompiledContentRuleListData.h:
+        (WebKit::WebCompiledContentRuleListData::WebCompiledContentRuleListData):
+        * UIProcess/API/APIContentRuleListStore.cpp:
+        (API::ContentRuleListMetaData::fileSize const):
+        (API::encodeContentRuleListMetaData):
+        (API::decodeContentRuleListMetaData):
+        (API::compiledToFile):
+        (API::createExtension):
+        (API::getContentRuleListSourceFromMappedFile):
+        * UIProcess/API/APIContentRuleListStore.h:
+
 2021-12-03  Eric Carlson  <[email protected]>
 
         DisplayCaptureSource doesn't initialize base class correctly

Modified: trunk/Source/WebKit/Shared/WebCompiledContentRuleList.cpp (286491 => 286492)


--- trunk/Source/WebKit/Shared/WebCompiledContentRuleList.cpp	2021-12-03 16:35:55 UTC (rev 286491)
+++ trunk/Source/WebKit/Shared/WebCompiledContentRuleList.cpp	2021-12-03 16:50:58 UTC (rev 286492)
@@ -46,7 +46,7 @@
 
 bool WebCompiledContentRuleList::conditionsApplyOnlyToDomain() const
 {
-    return *reinterpret_cast<const uint32_t*>(static_cast<const uint8_t*>(m_data.data->data()) + m_data.conditionsApplyOnlyToDomainOffset);
+    return m_data.conditionsApplyOnlyToDomain;
 }
 
 Span<const uint8_t> WebCompiledContentRuleList::filtersWithoutConditionsBytecode() const
@@ -64,6 +64,11 @@
     return spanWithOffsetAndLength(m_data.topURLFiltersBytecodeOffset, m_data.topURLFiltersBytecodeSize);
 }
 
+Span<const uint8_t> WebCompiledContentRuleList::frameURLFiltersBytecode() const
+{
+    return spanWithOffsetAndLength(m_data.frameURLFiltersBytecodeOffset, m_data.frameURLFiltersBytecodeSize);
+}
+
 Span<const uint8_t> WebCompiledContentRuleList::serializedActions() const
 {
     return spanWithOffsetAndLength(m_data.actionsOffset, m_data.actionsSize);

Modified: trunk/Source/WebKit/Shared/WebCompiledContentRuleList.h (286491 => 286492)


--- trunk/Source/WebKit/Shared/WebCompiledContentRuleList.h	2021-12-03 16:35:55 UTC (rev 286491)
+++ trunk/Source/WebKit/Shared/WebCompiledContentRuleList.h	2021-12-03 16:50:58 UTC (rev 286492)
@@ -46,6 +46,7 @@
     Span<const uint8_t> filtersWithoutConditionsBytecode() const final;
     Span<const uint8_t> filtersWithConditionsBytecode() const final;
     Span<const uint8_t> topURLFiltersBytecode() const final;
+    Span<const uint8_t> frameURLFiltersBytecode() const final;
     Span<const uint8_t> serializedActions() const final;
     bool conditionsApplyOnlyToDomain() const final;
     

Modified: trunk/Source/WebKit/Shared/WebCompiledContentRuleListData.cpp (286491 => 286492)


--- trunk/Source/WebKit/Shared/WebCompiledContentRuleListData.cpp	2021-12-03 16:35:55 UTC (rev 286491)
+++ trunk/Source/WebKit/Shared/WebCompiledContentRuleListData.cpp	2021-12-03 16:50:58 UTC (rev 286492)
@@ -49,7 +49,7 @@
 #endif
     encoder << SharedMemory::IPCHandle { WTFMove(handle), dataSize };
 
-    encoder << conditionsApplyOnlyToDomainOffset;
+    encoder << conditionsApplyOnlyToDomain;
     encoder << actionsOffset;
     encoder << actionsSize;
     encoder << filtersWithoutConditionsBytecodeOffset;
@@ -58,6 +58,8 @@
     encoder << filtersWithConditionsBytecodeSize;
     encoder << topURLFiltersBytecodeOffset;
     encoder << topURLFiltersBytecodeSize;
+    encoder << frameURLFiltersBytecodeOffset;
+    encoder << frameURLFiltersBytecodeSize;
 }
 
 std::optional<WebCompiledContentRuleListData> WebCompiledContentRuleListData::decode(IPC::Decoder& decoder)
@@ -74,55 +76,65 @@
     if (!data)
         return std::nullopt;
 
-    std::optional<unsigned> conditionsApplyOnlyToDomainOffset;
-    decoder >> conditionsApplyOnlyToDomainOffset;
-    if (!conditionsApplyOnlyToDomainOffset)
+    std::optional<bool> conditionsApplyOnlyToDomain;
+    decoder >> conditionsApplyOnlyToDomain;
+    if (!conditionsApplyOnlyToDomain)
         return std::nullopt;
 
-    std::optional<unsigned> actionsOffset;
+    std::optional<size_t> actionsOffset;
     decoder >> actionsOffset;
     if (!actionsOffset)
         return std::nullopt;
 
-    std::optional<unsigned> actionsSize;
+    std::optional<size_t> actionsSize;
     decoder >> actionsSize;
     if (!actionsSize)
         return std::nullopt;
 
-    std::optional<unsigned> filtersWithoutConditionsBytecodeOffset;
+    std::optional<size_t> filtersWithoutConditionsBytecodeOffset;
     decoder >> filtersWithoutConditionsBytecodeOffset;
     if (!filtersWithoutConditionsBytecodeOffset)
         return std::nullopt;
 
-    std::optional<unsigned> filtersWithoutConditionsBytecodeSize;
+    std::optional<size_t> filtersWithoutConditionsBytecodeSize;
     decoder >> filtersWithoutConditionsBytecodeSize;
     if (!filtersWithoutConditionsBytecodeSize)
         return std::nullopt;
 
-    std::optional<unsigned> filtersWithConditionsBytecodeOffset;
+    std::optional<size_t> filtersWithConditionsBytecodeOffset;
     decoder >> filtersWithConditionsBytecodeOffset;
     if (!filtersWithConditionsBytecodeOffset)
         return std::nullopt;
 
-    std::optional<unsigned> filtersWithConditionsBytecodeSize;
+    std::optional<size_t> filtersWithConditionsBytecodeSize;
     decoder >> filtersWithConditionsBytecodeSize;
     if (!filtersWithConditionsBytecodeSize)
         return std::nullopt;
 
-    std::optional<unsigned> topURLFiltersBytecodeOffset;
+    std::optional<size_t> topURLFiltersBytecodeOffset;
     decoder >> topURLFiltersBytecodeOffset;
     if (!topURLFiltersBytecodeOffset)
         return std::nullopt;
 
-    std::optional<unsigned> topURLFiltersBytecodeSize;
+    std::optional<size_t> topURLFiltersBytecodeSize;
     decoder >> topURLFiltersBytecodeSize;
     if (!topURLFiltersBytecodeSize)
         return std::nullopt;
 
+    std::optional<size_t> frameURLFiltersBytecodeOffset;
+    decoder >> frameURLFiltersBytecodeOffset;
+    if (!frameURLFiltersBytecodeOffset)
+        return std::nullopt;
+
+    std::optional<size_t> frameURLFiltersBytecodeSize;
+    decoder >> frameURLFiltersBytecodeSize;
+    if (!frameURLFiltersBytecodeSize)
+        return std::nullopt;
+
     return {{
         WTFMove(*identifier),
         data.releaseNonNull(),
-        WTFMove(*conditionsApplyOnlyToDomainOffset),
+        WTFMove(*conditionsApplyOnlyToDomain),
         WTFMove(*actionsOffset),
         WTFMove(*actionsSize),
         WTFMove(*filtersWithoutConditionsBytecodeOffset),
@@ -130,7 +142,9 @@
         WTFMove(*filtersWithConditionsBytecodeOffset),
         WTFMove(*filtersWithConditionsBytecodeSize),
         WTFMove(*topURLFiltersBytecodeOffset),
-        WTFMove(*topURLFiltersBytecodeSize)
+        WTFMove(*topURLFiltersBytecodeSize),
+        WTFMove(*frameURLFiltersBytecodeOffset),
+        WTFMove(*frameURLFiltersBytecodeSize)
     }};
 }
 

Modified: trunk/Source/WebKit/Shared/WebCompiledContentRuleListData.h (286491 => 286492)


--- trunk/Source/WebKit/Shared/WebCompiledContentRuleListData.h	2021-12-03 16:35:55 UTC (rev 286491)
+++ trunk/Source/WebKit/Shared/WebCompiledContentRuleListData.h	2021-12-03 16:50:58 UTC (rev 286492)
@@ -41,10 +41,10 @@
 
 class WebCompiledContentRuleListData {
 public:
-    WebCompiledContentRuleListData(String&& identifier, Ref<SharedMemory>&& data, unsigned conditionsApplyOnlyToDomainOffset, unsigned actionsOffset, unsigned actionsSize, unsigned filtersWithoutConditionsBytecodeOffset, unsigned filtersWithoutConditionsBytecodeSize, unsigned filtersWithConditionsBytecodeOffset, unsigned filtersWithConditionsBytecodeSize, unsigned topURLFiltersBytecodeOffset, unsigned topURLFiltersBytecodeSize)
+    WebCompiledContentRuleListData(String&& identifier, Ref<SharedMemory>&& data, bool conditionsApplyOnlyToDomain, size_t actionsOffset, size_t actionsSize, size_t filtersWithoutConditionsBytecodeOffset, size_t filtersWithoutConditionsBytecodeSize, size_t filtersWithConditionsBytecodeOffset, size_t filtersWithConditionsBytecodeSize, size_t topURLFiltersBytecodeOffset, size_t topURLFiltersBytecodeSize, size_t frameURLFiltersBytecodeOffset, size_t frameURLFiltersBytecodeSize)
         : identifier(WTFMove(identifier))
         , data(WTFMove(data))
-        , conditionsApplyOnlyToDomainOffset(conditionsApplyOnlyToDomainOffset)
+        , conditionsApplyOnlyToDomain(conditionsApplyOnlyToDomain)
         , actionsOffset(actionsOffset)
         , actionsSize(actionsSize)
         , filtersWithoutConditionsBytecodeOffset(filtersWithoutConditionsBytecodeOffset)
@@ -53,6 +53,8 @@
         , filtersWithConditionsBytecodeSize(filtersWithConditionsBytecodeSize)
         , topURLFiltersBytecodeOffset(topURLFiltersBytecodeOffset)
         , topURLFiltersBytecodeSize(topURLFiltersBytecodeSize)
+        , frameURLFiltersBytecodeOffset(frameURLFiltersBytecodeOffset)
+        , frameURLFiltersBytecodeSize(frameURLFiltersBytecodeSize)
     {
     }
 
@@ -61,15 +63,17 @@
 
     String identifier;
     Ref<SharedMemory> data;
-    unsigned conditionsApplyOnlyToDomainOffset { 0 };
-    unsigned actionsOffset { 0 };
-    unsigned actionsSize { 0 };
-    unsigned filtersWithoutConditionsBytecodeOffset { 0 };
-    unsigned filtersWithoutConditionsBytecodeSize { 0 };
-    unsigned filtersWithConditionsBytecodeOffset { 0 };
-    unsigned filtersWithConditionsBytecodeSize { 0 };
-    unsigned topURLFiltersBytecodeOffset { 0 };
-    unsigned topURLFiltersBytecodeSize { 0 };
+    bool conditionsApplyOnlyToDomain { 0 };
+    size_t actionsOffset { 0 };
+    size_t actionsSize { 0 };
+    size_t filtersWithoutConditionsBytecodeOffset { 0 };
+    size_t filtersWithoutConditionsBytecodeSize { 0 };
+    size_t filtersWithConditionsBytecodeOffset { 0 };
+    size_t filtersWithConditionsBytecodeSize { 0 };
+    size_t topURLFiltersBytecodeOffset { 0 };
+    size_t topURLFiltersBytecodeSize { 0 };
+    size_t frameURLFiltersBytecodeOffset { 0 };
+    size_t frameURLFiltersBytecodeSize { 0 };
 };
 
 }

Modified: trunk/Source/WebKit/UIProcess/API/APIContentRuleListStore.cpp (286491 => 286492)


--- trunk/Source/WebKit/UIProcess/API/APIContentRuleListStore.cpp	2021-12-03 16:35:55 UTC (rev 286491)
+++ trunk/Source/WebKit/UIProcess/API/APIContentRuleListStore.cpp	2021-12-03 16:50:58 UTC (rev 286492)
@@ -96,9 +96,15 @@
 
 // The size and offset of the densely packed bytes in the file, not sizeof and offsetof, which would
 // represent the size and offset of the structure in memory, possibly with compiler-added padding.
-const size_t ContentRuleListFileHeaderSize = 2 * sizeof(uint32_t) + 5 * sizeof(uint64_t);
-const size_t ConditionsApplyOnlyToDomainOffset = sizeof(uint32_t) + 5 * sizeof(uint64_t);
+const size_t CurrentVersionFileHeaderSize = 2 * sizeof(uint32_t) + 7 * sizeof(uint64_t);
 
+static size_t headerSize(uint32_t version)
+{
+    if (version < 12)
+        return 2 * sizeof(uint32_t) + 5 * sizeof(uint64_t);
+    return CurrentVersionFileHeaderSize;
+}
+
 struct ContentRuleListMetaData {
     uint32_t version { ContentRuleListStore::CurrentContentRuleListFileVersion };
     uint64_t sourceSize { 0 };
@@ -107,15 +113,18 @@
     uint64_t filtersWithConditionsBytecodeSize { 0 };
     uint64_t conditionedFiltersBytecodeSize { 0 };
     uint32_t conditionsApplyOnlyToDomain { false };
+    uint64_t frameURLFiltersBytecodeSize { 0 };
+    uint64_t unused { 0 }; // Additional space on disk reserved so we can add something without incrementing the version number.
     
     size_t fileSize() const
     {
-        return ContentRuleListFileHeaderSize
+        return headerSize(version)
             + sourceSize
             + actionsSize
             + filtersWithoutConditionsBytecodeSize
             + filtersWithConditionsBytecodeSize
-            + conditionedFiltersBytecodeSize;
+            + conditionedFiltersBytecodeSize
+            + frameURLFiltersBytecodeSize;
     }
 };
 
@@ -130,8 +139,10 @@
     encoder << metaData.filtersWithConditionsBytecodeSize;
     encoder << metaData.conditionedFiltersBytecodeSize;
     encoder << metaData.conditionsApplyOnlyToDomain;
+    encoder << metaData.frameURLFiltersBytecodeSize;
+    encoder << metaData.unused;
 
-    ASSERT(encoder.bufferSize() == ContentRuleListFileHeaderSize);
+    ASSERT(encoder.bufferSize() == CurrentVersionFileHeaderSize);
     return WebKit::NetworkCache::Data(encoder.buffer(), encoder.bufferSize());
 }
 
@@ -145,66 +156,70 @@
     function({ data.data(), data.size() });
 }
 
-template<typename T>
-static std::optional<ContentRuleListMetaData> decodeContentRuleListMetaData(const T& fileData)
+static std::optional<ContentRuleListMetaData> decodeContentRuleListMetaData(const WebKit::NetworkCache::Data& fileData)
 {
-    bool success = false;
     ContentRuleListMetaData metaData;
-    getData(fileData, [&metaData, &success, &fileData](Span<const uint8_t> span) {
-        // The file data should be mapped into one continuous memory segment so the size
-        // passed to the applier should always equal the data size.
-        if (span.size() != fileData.size())
-            return false;
+    auto span = fileData.span();
 
-        WTF::Persistence::Decoder decoder(span);
-        
-        std::optional<uint32_t> version;
-        decoder >> version;
-        if (!version)
-            return false;
-        metaData.version = WTFMove(*version);
+    WTF::Persistence::Decoder decoder(span);
+    
+    std::optional<uint32_t> version;
+    decoder >> version;
+    if (!version)
+        return std::nullopt;
+    metaData.version = WTFMove(*version);
 
-        std::optional<uint64_t> sourceSize;
-        decoder >> sourceSize;
-        if (!sourceSize)
-            return false;
-        metaData.sourceSize = WTFMove(*sourceSize);
+    std::optional<uint64_t> sourceSize;
+    decoder >> sourceSize;
+    if (!sourceSize)
+        return std::nullopt;
+    metaData.sourceSize = WTFMove(*sourceSize);
 
-        std::optional<uint64_t> actionsSize;
-        decoder >> actionsSize;
-        if (!actionsSize)
-            return false;
-        metaData.actionsSize = WTFMove(*actionsSize);
+    std::optional<uint64_t> actionsSize;
+    decoder >> actionsSize;
+    if (!actionsSize)
+        return std::nullopt;
+    metaData.actionsSize = WTFMove(*actionsSize);
 
-        std::optional<uint64_t> filtersWithoutConditionsBytecodeSize;
-        decoder >> filtersWithoutConditionsBytecodeSize;
-        if (!filtersWithoutConditionsBytecodeSize)
-            return false;
-        metaData.filtersWithoutConditionsBytecodeSize = WTFMove(*filtersWithoutConditionsBytecodeSize);
+    std::optional<uint64_t> filtersWithoutConditionsBytecodeSize;
+    decoder >> filtersWithoutConditionsBytecodeSize;
+    if (!filtersWithoutConditionsBytecodeSize)
+        return std::nullopt;
+    metaData.filtersWithoutConditionsBytecodeSize = WTFMove(*filtersWithoutConditionsBytecodeSize);
 
-        std::optional<uint64_t> filtersWithConditionsBytecodeSize;
-        decoder >> filtersWithConditionsBytecodeSize;
-        if (!filtersWithConditionsBytecodeSize)
-            return false;
-        metaData.filtersWithConditionsBytecodeSize = WTFMove(*filtersWithConditionsBytecodeSize);
+    std::optional<uint64_t> filtersWithConditionsBytecodeSize;
+    decoder >> filtersWithConditionsBytecodeSize;
+    if (!filtersWithConditionsBytecodeSize)
+        return std::nullopt;
+    metaData.filtersWithConditionsBytecodeSize = WTFMove(*filtersWithConditionsBytecodeSize);
 
-        std::optional<uint64_t> conditionedFiltersBytecodeSize;
-        decoder >> conditionedFiltersBytecodeSize;
-        if (!conditionedFiltersBytecodeSize)
-            return false;
-        metaData.conditionedFiltersBytecodeSize = WTFMove(*conditionedFiltersBytecodeSize);
+    std::optional<uint64_t> conditionedFiltersBytecodeSize;
+    decoder >> conditionedFiltersBytecodeSize;
+    if (!conditionedFiltersBytecodeSize)
+        return std::nullopt;
+    metaData.conditionedFiltersBytecodeSize = WTFMove(*conditionedFiltersBytecodeSize);
 
-        std::optional<uint32_t> conditionsApplyOnlyToDomain;
-        decoder >> conditionsApplyOnlyToDomain;
-        if (!conditionsApplyOnlyToDomain)
-            return false;
-        metaData.conditionsApplyOnlyToDomain = WTFMove(*conditionsApplyOnlyToDomain);
+    std::optional<uint32_t> conditionsApplyOnlyToDomain;
+    decoder >> conditionsApplyOnlyToDomain;
+    if (!conditionsApplyOnlyToDomain)
+        return std::nullopt;
+    metaData.conditionsApplyOnlyToDomain = WTFMove(*conditionsApplyOnlyToDomain);
 
-        success = true;
-        return false;
-    });
-    if (!success)
+    if (metaData.version < 12)
+        return metaData;
+
+    std::optional<uint64_t> frameURLFiltersBytecodeSize;
+    decoder >> frameURLFiltersBytecodeSize;
+    if (!frameURLFiltersBytecodeSize)
         return std::nullopt;
+    metaData.frameURLFiltersBytecodeSize = WTFMove(*frameURLFiltersBytecodeSize);
+    
+    std::optional<uint64_t> unused;
+    decoder >> unused;
+    if (!unused)
+        return std::nullopt;
+    metaData.unused = 0;
+
     return metaData;
 }
 
@@ -359,7 +374,7 @@
         return makeUnexpected(ContentRuleListStore::Error::CompileFailed);
     }
     
-    char invalidHeader[ContentRuleListFileHeaderSize];
+    char invalidHeader[CurrentVersionFileHeaderSize];
     memset(invalidHeader, 0xFF, sizeof(invalidHeader));
     // This header will be rewritten in CompilationClient::finalize.
     if (writeToFile(temporaryFileHandle, invalidHeader, sizeof(invalidHeader)) == -1) {
@@ -410,11 +425,11 @@
     // has been already mapped, therefore tryCreateSharedMemory() cannot fail.
     RELEASE_ASSERT(sharedMemory);
 
-    const size_t headerAndSourceSize = ContentRuleListFileHeaderSize + data.metaData.sourceSize;
+    const size_t headerAndSourceSize = headerSize(data.metaData.version) + data.metaData.sourceSize;
     auto compiledContentRuleListData = WebKit::WebCompiledContentRuleListData(
         WTF::String(identifier),
         sharedMemory.releaseNonNull(),
-        ConditionsApplyOnlyToDomainOffset,
+        data.metaData.conditionsApplyOnlyToDomain,
         headerAndSourceSize,
         data.metaData.actionsSize,
         headerAndSourceSize
@@ -428,7 +443,13 @@
             + data.metaData.actionsSize
             + data.metaData.filtersWithoutConditionsBytecodeSize
             + data.metaData.filtersWithConditionsBytecodeSize,
-        data.metaData.conditionedFiltersBytecodeSize
+        data.metaData.conditionedFiltersBytecodeSize,
+        headerAndSourceSize
+            + data.metaData.actionsSize
+            + data.metaData.filtersWithoutConditionsBytecodeSize
+            + data.metaData.filtersWithConditionsBytecodeSize
+            + data.metaData.conditionedFiltersBytecodeSize,
+        data.metaData.frameURLFiltersBytecodeSize
     );
     auto compiledContentRuleList = WebKit::WebCompiledContentRuleList::create(WTFMove(compiledContentRuleListData));
     return API::ContentRuleList::create(WTFMove(compiledContentRuleList), WTFMove(data.data));
@@ -442,10 +463,13 @@
     case 9:
     case 10:
     case 11:
+    case 12:
         if (!mappedData.metaData.sourceSize)
             return { };
-        bool is8Bit = mappedData.data.data()[ContentRuleListFileHeaderSize];
-        size_t start = ContentRuleListFileHeaderSize + sizeof(bool);
+            
+        auto headerSizeBytes = headerSize(mappedData.metaData.version);
+        bool is8Bit = mappedData.data.data()[headerSizeBytes];
+        size_t start = headerSizeBytes + sizeof(bool);
         size_t length = mappedData.metaData.sourceSize - sizeof(bool);
         if (is8Bit)
             return WTF::String(mappedData.data.data() + start, length);

Modified: trunk/Source/WebKit/UIProcess/API/APIContentRuleListStore.h (286491 => 286492)


--- trunk/Source/WebKit/UIProcess/API/APIContentRuleListStore.h	2021-12-03 16:35:55 UTC (rev 286491)
+++ trunk/Source/WebKit/UIProcess/API/APIContentRuleListStore.h	2021-12-03 16:50:58 UTC (rev 286492)
@@ -54,10 +54,9 @@
 #if ENABLE(CONTENT_EXTENSIONS)
     // This should be incremented every time a functional change is made to the bytecode, file format, etc.
     // to prevent crashing while loading old data.
-    // Also update ContentRuleListStore::getContentRuleListSource to be able to find the original JSON
+    // Also update getContentRuleListSourceFromMappedFile to be able to find the original JSON
     // source from old versions.
-    // Update getContentRuleListSourceFromMappedFile with this.
-    static constexpr uint32_t CurrentContentRuleListFileVersion = 11;
+    static constexpr uint32_t CurrentContentRuleListFileVersion = 12;
 
     static ContentRuleListStore& defaultStore();
     static Ref<ContentRuleListStore> storeWithPath(const WTF::String& storePath);

Modified: trunk/Tools/ChangeLog (286491 => 286492)


--- trunk/Tools/ChangeLog	2021-12-03 16:35:55 UTC (rev 286491)
+++ trunk/Tools/ChangeLog	2021-12-03 16:50:58 UTC (rev 286492)
@@ -1,3 +1,12 @@
+2021-12-03  Alex Christensen  <[email protected]>
+
+        Add room for more bytecode in WKContentRuleList file format
+        https://bugs.webkit.org/show_bug.cgi?id=233780
+
+        Reviewed by Tim Hatcher.
+
+        * TestWebKitAPI/Tests/WebCore/ContentExtensions.cpp:
+
 2021-12-03  Youenn Fablet  <[email protected]>
 
         Persist NavigationPreloadState in service worker registration database

Modified: trunk/Tools/TestWebKitAPI/Tests/WebCore/ContentExtensions.cpp (286491 => 286492)


--- trunk/Tools/TestWebKitAPI/Tests/WebCore/ContentExtensions.cpp	2021-12-03 16:35:55 UTC (rev 286491)
+++ trunk/Tools/TestWebKitAPI/Tests/WebCore/ContentExtensions.cpp	2021-12-03 16:50:58 UTC (rev 286492)
@@ -66,6 +66,7 @@
     Vector<ContentExtensions::DFABytecode> filtersWithoutConditions;
     Vector<ContentExtensions::DFABytecode> filtersWithConditions;
     Vector<ContentExtensions::DFABytecode> topURLFilters;
+    Vector<ContentExtensions::DFABytecode> frameURLFilters;
     bool conditionsApplyOnlyToDomain { false };
 };
 
@@ -147,6 +148,7 @@
     Span<const uint8_t> filtersWithoutConditionsBytecode() const final { return { m_data.filtersWithoutConditions.data(), m_data.filtersWithoutConditions.size() }; }
     Span<const uint8_t> filtersWithConditionsBytecode() const final { return { m_data.filtersWithConditions.data(), m_data.filtersWithConditions.size() }; }
     Span<const uint8_t> topURLFiltersBytecode() const final { return { m_data.topURLFilters.data(), m_data.topURLFilters.size() }; }
+    Span<const uint8_t> frameURLFiltersBytecode() const final { return { m_data.frameURLFilters.data(), m_data.frameURLFilters.size() }; }
     bool conditionsApplyOnlyToDomain() const final { return m_data.conditionsApplyOnlyToDomain; }
 
     InMemoryCompiledContentExtension(CompiledContentExtensionData&& data)
@@ -966,7 +968,7 @@
     ASSERT_EQ(sequenceInstances(data.actions, "AAA"), 2);
     ASSERT_EQ(sequenceInstances(data.actions, "GGG"), 1);
 
-    ASSERT_EQ(data.actions.size(), 78u);
+    ASSERT_EQ(data.actions.size(), 72u);
     ASSERT_EQ(data.filtersWithoutConditions.size(), 288u);
     ASSERT_EQ(data.filtersWithConditions.size(), 5u);
     ASSERT_EQ(data.topURLFilters.size(), 5u);

Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKContentExtensionStore.mm (286491 => 286492)


--- trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKContentExtensionStore.mm	2021-12-03 16:35:55 UTC (rev 286491)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKContentExtensionStore.mm	2021-12-03 16:50:58 UTC (rev 286492)
@@ -252,7 +252,7 @@
 
     NSData *data = "" dataWithContentsOfURL:[tempDir URLByAppendingPathComponent:fileName]];
     EXPECT_NOT_NULL(data);
-    EXPECT_EQ(data.length, 225u);
+    EXPECT_EQ(data.length, 241u);
     
     __block bool doneCheckingSource = false;
     [store _getContentRuleListSourceForIdentifier:identifier completionHandler:^(NSString *source) {
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to