Changeset 286492 in webkit


Ignore:
Timestamp:
Dec 3, 2021 8:50:58 AM (2 years ago)
Author:
achristensen@apple.com
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:
Location:
trunk
Files:
15 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r286491 r286492  
     12021-12-03  Alex Christensen  <achristensen@webkit.org>
     2
     3        Add room for more bytecode in WKContentRuleList file format
     4        https://bugs.webkit.org/show_bug.cgi?id=233780
     5
     6        Reviewed by Tim Hatcher.
     7
     8        For rdar://72203352 we will need more bytecode.  This adds room for it without putting anything there yet.
     9        As long as we are updating the version number and forcing a recompile, I'm making the string serialization
     10        more like the other action serializations to be able to simplify the interpreter later.  I'm also shifting
     11        around a few bits in the bytecode to be more organized and reasonable.
     12
     13        * contentextensions/CompiledContentExtension.h:
     14        * contentextensions/ContentExtensionStringSerialization.cpp:
     15        (WebCore::ContentExtensions::deserializeString):
     16        (WebCore::ContentExtensions::serializeString):
     17        (WebCore::ContentExtensions::stringSerializedLength):
     18        * contentextensions/DFABytecode.h:
     19        (WebCore::ContentExtensions::smallestPossibleJumpSize): Deleted.
     20        * contentextensions/DFABytecodeCompiler.cpp:
     21        (WebCore::ContentExtensions::smallestPossibleJumpSize):
     22
    1232021-12-03  Eric Carlson  <eric.carlson@apple.com>
    224
  • trunk/Source/WebCore/contentextensions/CompiledContentExtension.h

    r286402 r286492  
    4242    virtual Span<const uint8_t> filtersWithConditionsBytecode() const = 0;
    4343    virtual Span<const uint8_t> topURLFiltersBytecode() const = 0;
     44    virtual Span<const uint8_t> frameURLFiltersBytecode() const = 0;
    4445    virtual Span<const uint8_t> serializedActions() const = 0;
    4546    virtual bool conditionsApplyOnlyToDomain() const = 0;
  • trunk/Source/WebCore/contentextensions/ContentExtensionStringSerialization.cpp

    r285350 r286492  
    3333String deserializeString(Span<const uint8_t> span)
    3434{
    35     const auto* actions = span.data();
    36     const auto actionsLength = span.size();
    37     auto prefixLength = sizeof(uint32_t) + sizeof(bool);
    38     auto stringStartIndex = prefixLength;
    39     RELEASE_ASSERT(actionsLength >= stringStartIndex);
    40     uint32_t stringLength = *reinterpret_cast<const uint32_t*>(actions);
    41     bool wideCharacters = actions[sizeof(uint32_t)];
    42 
    43     if (wideCharacters) {
    44         RELEASE_ASSERT(actionsLength >= stringStartIndex + stringLength * sizeof(UChar));
    45         return String(reinterpret_cast<const UChar*>(&actions[stringStartIndex]), stringLength);
    46     }
    47     RELEASE_ASSERT(actionsLength >= stringStartIndex + stringLength * sizeof(LChar));
    48     return String(reinterpret_cast<const LChar*>(&actions[stringStartIndex]), stringLength);
     35    auto serializedLength = *reinterpret_cast<const uint32_t*>(span.data());
     36    return String::fromUTF8(span.data() + sizeof(uint32_t), serializedLength - sizeof(uint32_t));
    4937}
    5038
    5139void serializeString(Vector<uint8_t>& actions, const String& string)
    5240{
    53     // Append Selector length (4 bytes).
    54     uint32_t stringLength = string.length();
    55     actions.grow(actions.size() + sizeof(uint32_t));
    56     *reinterpret_cast<uint32_t*>(&actions[actions.size() - sizeof(uint32_t)]) = stringLength;
    57     bool wideCharacters = !string.is8Bit();
    58     actions.append(wideCharacters);
    59     // Append Selector.
    60     if (wideCharacters) {
    61         uint32_t startIndex = actions.size();
    62         actions.grow(actions.size() + sizeof(UChar) * stringLength);
    63         for (uint32_t i = 0; i < stringLength; ++i)
    64             *reinterpret_cast<UChar*>(&actions[startIndex + i * sizeof(UChar)]) = string[i];
    65     } else {
    66         for (uint32_t i = 0; i < stringLength; ++i)
    67             actions.append(string[i]);
    68     }
     41    auto utf8 = string.utf8();
     42    uint32_t serializedLength = sizeof(uint32_t) + utf8.length();
     43    actions.reserveCapacity(actions.size() + serializedLength);
     44    actions.uncheckedAppend(Span<const uint8_t> { reinterpret_cast<const uint8_t*>(&serializedLength), sizeof(serializedLength) });
     45    actions.uncheckedAppend(Span<const uint8_t> { reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length() });
    6946}
    7047
    7148size_t stringSerializedLength(Span<const uint8_t> span)
    7249{
    73     constexpr auto prefixLength = sizeof(uint32_t) + sizeof(bool);
    74     RELEASE_ASSERT(span.size() >= prefixLength);
    75     auto stringLength = *reinterpret_cast<const uint32_t*>(span.data());
    76     bool wideCharacters = span[sizeof(uint32_t)];
    77     if (wideCharacters)
    78         return prefixLength + stringLength * sizeof(UChar);
    79     return prefixLength + stringLength * sizeof(LChar);
     50    return *reinterpret_cast<const uint32_t*>(span.data());
    8051}
    8152
  • trunk/Source/WebCore/contentextensions/DFABytecode.h

    r286383 r286492  
    7979// The last four bits contain the instruction type.
    8080static constexpr uint8_t DFABytecodeInstructionMask = 0x0F;
    81 static constexpr uint8_t DFABytecodeJumpSizeMask = 0xF0;
     81static constexpr uint8_t DFABytecodeJumpSizeMask = 0x30;
    8282static constexpr uint8_t DFABytecodeFlagsSizeMask = 0x30;
    8383static constexpr uint8_t DFABytecodeActionSizeMask = 0xC0;
     
    8888// DFABytecodeFlagsSize and DFABytecodeActionSize are stored in the top four bits of the DFABytecodeInstructions that have flags and actions.
    8989enum class DFABytecodeFlagsSize : uint8_t {
    90     UInt8 = 0x10,
    91     UInt16 = 0x00, // Needs to be zero to be binary compatible with bytecode compiled with fixed-size flags.
     90    UInt8 = 0x00,
     91    UInt16 = 0x10,
    9292    UInt24 = 0x20,
    9393};
    9494enum class DFABytecodeActionSize : uint8_t {
    95     UInt8 = 0x40,
    96     UInt16 = 0x80,
    97     UInt24 = 0xC0,
    98     UInt32 = 0x00, // Needs to be zero to be binary compatible with bytecode compiled with fixed-size actions.
     95    UInt8 = 0x00,
     96    UInt16 = 0x40,
     97    UInt24 = 0x80,
     98    UInt32 = 0xC0,
    9999};
    100100
    101101// A DFABytecodeJumpSize is stored in the top four bits of the DFABytecodeInstructions that have a jump.
    102102enum class DFABytecodeJumpSize : uint8_t {
    103     Int8 = 0x10,
    104     Int16 = 0x20,
    105     Int24 = 0x30,
    106     Int32 = 0x40,
     103    Int8 = 0x00,
     104    Int16 = 0x10,
     105    Int24 = 0x20,
     106    Int32 = 0x30,
    107107};
    108108static constexpr int32_t UInt24Max = (1 << 24) - 1;
     
    112112static constexpr size_t UInt24Size = 3;
    113113
    114 static inline DFABytecodeJumpSize smallestPossibleJumpSize(int32_t longestPossibleJump)
    115 {
    116     if (longestPossibleJump <= std::numeric_limits<int8_t>::max() && longestPossibleJump >= std::numeric_limits<int8_t>::min())
    117         return DFABytecodeJumpSize::Int8;
    118     if (longestPossibleJump <= std::numeric_limits<int16_t>::max() && longestPossibleJump >= std::numeric_limits<int16_t>::min())
    119         return DFABytecodeJumpSize::Int16;
    120     if (longestPossibleJump <= Int24Max && longestPossibleJump >= Int24Min)
    121         return DFABytecodeJumpSize::Int24;
    122     return DFABytecodeJumpSize::Int32;
    123 }
    124 
    125114} // namespace WebCore::ContentExtensions
    126115
  • trunk/Source/WebCore/contentextensions/DFABytecodeCompiler.cpp

    r286383 r286492  
    152152    return m_nodeStartOffsets[destinationNodeIndex] - instructionLocation;
    153153}
    154    
     154
     155static DFABytecodeJumpSize smallestPossibleJumpSize(int32_t longestPossibleJump)
     156{
     157    if (longestPossibleJump <= std::numeric_limits<int8_t>::max() && longestPossibleJump >= std::numeric_limits<int8_t>::min())
     158        return DFABytecodeJumpSize::Int8;
     159    if (longestPossibleJump <= std::numeric_limits<int16_t>::max() && longestPossibleJump >= std::numeric_limits<int16_t>::min())
     160        return DFABytecodeJumpSize::Int16;
     161    if (longestPossibleJump <= Int24Max && longestPossibleJump >= Int24Min)
     162        return DFABytecodeJumpSize::Int24;
     163    return DFABytecodeJumpSize::Int32;
     164}
     165
    155166void DFABytecodeCompiler::emitJump(uint32_t sourceNodeIndex, uint32_t destinationNodeIndex)
    156167{
  • trunk/Source/WebKit/ChangeLog

    r286491 r286492  
     12021-12-03  Alex Christensen  <achristensen@webkit.org>
     2
     3        Add room for more bytecode in WKContentRuleList file format
     4        https://bugs.webkit.org/show_bug.cgi?id=233780
     5
     6        Reviewed by Tim Hatcher.
     7
     8        * Shared/WebCompiledContentRuleList.cpp:
     9        (WebKit::WebCompiledContentRuleList::frameURLFiltersBytecode const):
     10        * Shared/WebCompiledContentRuleList.h:
     11        * Shared/WebCompiledContentRuleListData.cpp:
     12        (WebKit::WebCompiledContentRuleListData::encode const):
     13        (WebKit::WebCompiledContentRuleListData::decode):
     14        * Shared/WebCompiledContentRuleListData.h:
     15        (WebKit::WebCompiledContentRuleListData::WebCompiledContentRuleListData):
     16        * UIProcess/API/APIContentRuleListStore.cpp:
     17        (API::ContentRuleListMetaData::fileSize const):
     18        (API::encodeContentRuleListMetaData):
     19        (API::decodeContentRuleListMetaData):
     20        (API::compiledToFile):
     21        (API::createExtension):
     22        (API::getContentRuleListSourceFromMappedFile):
     23        * UIProcess/API/APIContentRuleListStore.h:
     24
    1252021-12-03  Eric Carlson  <eric.carlson@apple.com>
    226
  • trunk/Source/WebKit/Shared/WebCompiledContentRuleList.cpp

    r286402 r286492  
    4747bool WebCompiledContentRuleList::conditionsApplyOnlyToDomain() const
    4848{
    49     return *reinterpret_cast<const uint32_t*>(static_cast<const uint8_t*>(m_data.data->data()) + m_data.conditionsApplyOnlyToDomainOffset);
     49    return m_data.conditionsApplyOnlyToDomain;
    5050}
    5151
     
    6565}
    6666
     67Span<const uint8_t> WebCompiledContentRuleList::frameURLFiltersBytecode() const
     68{
     69    return spanWithOffsetAndLength(m_data.frameURLFiltersBytecodeOffset, m_data.frameURLFiltersBytecodeSize);
     70}
     71
    6772Span<const uint8_t> WebCompiledContentRuleList::serializedActions() const
    6873{
  • trunk/Source/WebKit/Shared/WebCompiledContentRuleList.h

    r286402 r286492  
    4747    Span<const uint8_t> filtersWithConditionsBytecode() const final;
    4848    Span<const uint8_t> topURLFiltersBytecode() const final;
     49    Span<const uint8_t> frameURLFiltersBytecode() const final;
    4950    Span<const uint8_t> serializedActions() const final;
    5051    bool conditionsApplyOnlyToDomain() const final;
  • trunk/Source/WebKit/Shared/WebCompiledContentRuleListData.cpp

    r286084 r286492  
    5050    encoder << SharedMemory::IPCHandle { WTFMove(handle), dataSize };
    5151
    52     encoder << conditionsApplyOnlyToDomainOffset;
     52    encoder << conditionsApplyOnlyToDomain;
    5353    encoder << actionsOffset;
    5454    encoder << actionsSize;
     
    5959    encoder << topURLFiltersBytecodeOffset;
    6060    encoder << topURLFiltersBytecodeSize;
     61    encoder << frameURLFiltersBytecodeOffset;
     62    encoder << frameURLFiltersBytecodeSize;
    6163}
    6264
     
    7577        return std::nullopt;
    7678
    77     std::optional<unsigned> conditionsApplyOnlyToDomainOffset;
    78     decoder >> conditionsApplyOnlyToDomainOffset;
    79     if (!conditionsApplyOnlyToDomainOffset)
     79    std::optional<bool> conditionsApplyOnlyToDomain;
     80    decoder >> conditionsApplyOnlyToDomain;
     81    if (!conditionsApplyOnlyToDomain)
    8082        return std::nullopt;
    8183
    82     std::optional<unsigned> actionsOffset;
     84    std::optional<size_t> actionsOffset;
    8385    decoder >> actionsOffset;
    8486    if (!actionsOffset)
    8587        return std::nullopt;
    8688
    87     std::optional<unsigned> actionsSize;
     89    std::optional<size_t> actionsSize;
    8890    decoder >> actionsSize;
    8991    if (!actionsSize)
    9092        return std::nullopt;
    9193
    92     std::optional<unsigned> filtersWithoutConditionsBytecodeOffset;
     94    std::optional<size_t> filtersWithoutConditionsBytecodeOffset;
    9395    decoder >> filtersWithoutConditionsBytecodeOffset;
    9496    if (!filtersWithoutConditionsBytecodeOffset)
    9597        return std::nullopt;
    9698
    97     std::optional<unsigned> filtersWithoutConditionsBytecodeSize;
     99    std::optional<size_t> filtersWithoutConditionsBytecodeSize;
    98100    decoder >> filtersWithoutConditionsBytecodeSize;
    99101    if (!filtersWithoutConditionsBytecodeSize)
    100102        return std::nullopt;
    101103
    102     std::optional<unsigned> filtersWithConditionsBytecodeOffset;
     104    std::optional<size_t> filtersWithConditionsBytecodeOffset;
    103105    decoder >> filtersWithConditionsBytecodeOffset;
    104106    if (!filtersWithConditionsBytecodeOffset)
    105107        return std::nullopt;
    106108
    107     std::optional<unsigned> filtersWithConditionsBytecodeSize;
     109    std::optional<size_t> filtersWithConditionsBytecodeSize;
    108110    decoder >> filtersWithConditionsBytecodeSize;
    109111    if (!filtersWithConditionsBytecodeSize)
    110112        return std::nullopt;
    111113
    112     std::optional<unsigned> topURLFiltersBytecodeOffset;
     114    std::optional<size_t> topURLFiltersBytecodeOffset;
    113115    decoder >> topURLFiltersBytecodeOffset;
    114116    if (!topURLFiltersBytecodeOffset)
    115117        return std::nullopt;
    116118
    117     std::optional<unsigned> topURLFiltersBytecodeSize;
     119    std::optional<size_t> topURLFiltersBytecodeSize;
    118120    decoder >> topURLFiltersBytecodeSize;
    119121    if (!topURLFiltersBytecodeSize)
     122        return std::nullopt;
     123
     124    std::optional<size_t> frameURLFiltersBytecodeOffset;
     125    decoder >> frameURLFiltersBytecodeOffset;
     126    if (!frameURLFiltersBytecodeOffset)
     127        return std::nullopt;
     128
     129    std::optional<size_t> frameURLFiltersBytecodeSize;
     130    decoder >> frameURLFiltersBytecodeSize;
     131    if (!frameURLFiltersBytecodeSize)
    120132        return std::nullopt;
    121133
     
    123135        WTFMove(*identifier),
    124136        data.releaseNonNull(),
    125         WTFMove(*conditionsApplyOnlyToDomainOffset),
     137        WTFMove(*conditionsApplyOnlyToDomain),
    126138        WTFMove(*actionsOffset),
    127139        WTFMove(*actionsSize),
     
    131143        WTFMove(*filtersWithConditionsBytecodeSize),
    132144        WTFMove(*topURLFiltersBytecodeOffset),
    133         WTFMove(*topURLFiltersBytecodeSize)
     145        WTFMove(*topURLFiltersBytecodeSize),
     146        WTFMove(*frameURLFiltersBytecodeOffset),
     147        WTFMove(*frameURLFiltersBytecodeSize)
    134148    }};
    135149}
  • trunk/Source/WebKit/Shared/WebCompiledContentRuleListData.h

    r286084 r286492  
    4242class WebCompiledContentRuleListData {
    4343public:
    44     WebCompiledContentRuleListData(String&& identifier, Ref<SharedMemory>&& data, unsigned conditionsApplyOnlyToDomainOffset, unsigned actionsOffset, unsigned actionsSize, unsigned filtersWithoutConditionsBytecodeOffset, unsigned filtersWithoutConditionsBytecodeSize, unsigned filtersWithConditionsBytecodeOffset, unsigned filtersWithConditionsBytecodeSize, unsigned topURLFiltersBytecodeOffset, unsigned topURLFiltersBytecodeSize)
     44    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)
    4545        : identifier(WTFMove(identifier))
    4646        , data(WTFMove(data))
    47         , conditionsApplyOnlyToDomainOffset(conditionsApplyOnlyToDomainOffset)
     47        , conditionsApplyOnlyToDomain(conditionsApplyOnlyToDomain)
    4848        , actionsOffset(actionsOffset)
    4949        , actionsSize(actionsSize)
     
    5454        , topURLFiltersBytecodeOffset(topURLFiltersBytecodeOffset)
    5555        , topURLFiltersBytecodeSize(topURLFiltersBytecodeSize)
     56        , frameURLFiltersBytecodeOffset(frameURLFiltersBytecodeOffset)
     57        , frameURLFiltersBytecodeSize(frameURLFiltersBytecodeSize)
    5658    {
    5759    }
     
    6264    String identifier;
    6365    Ref<SharedMemory> data;
    64     unsigned conditionsApplyOnlyToDomainOffset { 0 };
    65     unsigned actionsOffset { 0 };
    66     unsigned actionsSize { 0 };
    67     unsigned filtersWithoutConditionsBytecodeOffset { 0 };
    68     unsigned filtersWithoutConditionsBytecodeSize { 0 };
    69     unsigned filtersWithConditionsBytecodeOffset { 0 };
    70     unsigned filtersWithConditionsBytecodeSize { 0 };
    71     unsigned topURLFiltersBytecodeOffset { 0 };
    72     unsigned topURLFiltersBytecodeSize { 0 };
     66    bool conditionsApplyOnlyToDomain { 0 };
     67    size_t actionsOffset { 0 };
     68    size_t actionsSize { 0 };
     69    size_t filtersWithoutConditionsBytecodeOffset { 0 };
     70    size_t filtersWithoutConditionsBytecodeSize { 0 };
     71    size_t filtersWithConditionsBytecodeOffset { 0 };
     72    size_t filtersWithConditionsBytecodeSize { 0 };
     73    size_t topURLFiltersBytecodeOffset { 0 };
     74    size_t topURLFiltersBytecodeSize { 0 };
     75    size_t frameURLFiltersBytecodeOffset { 0 };
     76    size_t frameURLFiltersBytecodeSize { 0 };
    7377};
    7478
  • trunk/Source/WebKit/UIProcess/API/APIContentRuleListStore.cpp

    r286084 r286492  
    9797// The size and offset of the densely packed bytes in the file, not sizeof and offsetof, which would
    9898// represent the size and offset of the structure in memory, possibly with compiler-added padding.
    99 const size_t ContentRuleListFileHeaderSize = 2 * sizeof(uint32_t) + 5 * sizeof(uint64_t);
    100 const size_t ConditionsApplyOnlyToDomainOffset = sizeof(uint32_t) + 5 * sizeof(uint64_t);
     99const size_t CurrentVersionFileHeaderSize = 2 * sizeof(uint32_t) + 7 * sizeof(uint64_t);
     100
     101static size_t headerSize(uint32_t version)
     102{
     103    if (version < 12)
     104        return 2 * sizeof(uint32_t) + 5 * sizeof(uint64_t);
     105    return CurrentVersionFileHeaderSize;
     106}
    101107
    102108struct ContentRuleListMetaData {
     
    108114    uint64_t conditionedFiltersBytecodeSize { 0 };
    109115    uint32_t conditionsApplyOnlyToDomain { false };
     116    uint64_t frameURLFiltersBytecodeSize { 0 };
     117    uint64_t unused { 0 }; // Additional space on disk reserved so we can add something without incrementing the version number.
    110118   
    111119    size_t fileSize() const
    112120    {
    113         return ContentRuleListFileHeaderSize
     121        return headerSize(version)
    114122            + sourceSize
    115123            + actionsSize
    116124            + filtersWithoutConditionsBytecodeSize
    117125            + filtersWithConditionsBytecodeSize
    118             + conditionedFiltersBytecodeSize;
     126            + conditionedFiltersBytecodeSize
     127            + frameURLFiltersBytecodeSize;
    119128    }
    120129};
     
    131140    encoder << metaData.conditionedFiltersBytecodeSize;
    132141    encoder << metaData.conditionsApplyOnlyToDomain;
    133 
    134     ASSERT(encoder.bufferSize() == ContentRuleListFileHeaderSize);
     142    encoder << metaData.frameURLFiltersBytecodeSize;
     143    encoder << metaData.unused;
     144
     145    ASSERT(encoder.bufferSize() == CurrentVersionFileHeaderSize);
    135146    return WebKit::NetworkCache::Data(encoder.buffer(), encoder.bufferSize());
    136147}
     
    146157}
    147158
    148 template<typename T>
    149 static std::optional<ContentRuleListMetaData> decodeContentRuleListMetaData(const T& fileData)
    150 {
    151     bool success = false;
     159static std::optional<ContentRuleListMetaData> decodeContentRuleListMetaData(const WebKit::NetworkCache::Data& fileData)
     160{
    152161    ContentRuleListMetaData metaData;
    153     getData(fileData, [&metaData, &success, &fileData](Span<const uint8_t> span) {
    154         // The file data should be mapped into one continuous memory segment so the size
    155         // passed to the applier should always equal the data size.
    156         if (span.size() != fileData.size())
    157             return false;
    158 
    159         WTF::Persistence::Decoder decoder(span);
    160        
    161         std::optional<uint32_t> version;
    162         decoder >> version;
    163         if (!version)
    164             return false;
    165         metaData.version = WTFMove(*version);
    166 
    167         std::optional<uint64_t> sourceSize;
    168         decoder >> sourceSize;
    169         if (!sourceSize)
    170             return false;
    171         metaData.sourceSize = WTFMove(*sourceSize);
    172 
    173         std::optional<uint64_t> actionsSize;
    174         decoder >> actionsSize;
    175         if (!actionsSize)
    176             return false;
    177         metaData.actionsSize = WTFMove(*actionsSize);
    178 
    179         std::optional<uint64_t> filtersWithoutConditionsBytecodeSize;
    180         decoder >> filtersWithoutConditionsBytecodeSize;
    181         if (!filtersWithoutConditionsBytecodeSize)
    182             return false;
    183         metaData.filtersWithoutConditionsBytecodeSize = WTFMove(*filtersWithoutConditionsBytecodeSize);
    184 
    185         std::optional<uint64_t> filtersWithConditionsBytecodeSize;
    186         decoder >> filtersWithConditionsBytecodeSize;
    187         if (!filtersWithConditionsBytecodeSize)
    188             return false;
    189         metaData.filtersWithConditionsBytecodeSize = WTFMove(*filtersWithConditionsBytecodeSize);
    190 
    191         std::optional<uint64_t> conditionedFiltersBytecodeSize;
    192         decoder >> conditionedFiltersBytecodeSize;
    193         if (!conditionedFiltersBytecodeSize)
    194             return false;
    195         metaData.conditionedFiltersBytecodeSize = WTFMove(*conditionedFiltersBytecodeSize);
    196 
    197         std::optional<uint32_t> conditionsApplyOnlyToDomain;
    198         decoder >> conditionsApplyOnlyToDomain;
    199         if (!conditionsApplyOnlyToDomain)
    200             return false;
    201         metaData.conditionsApplyOnlyToDomain = WTFMove(*conditionsApplyOnlyToDomain);
    202 
    203         success = true;
    204         return false;
    205     });
    206     if (!success)
    207         return std::nullopt;
     162    auto span = fileData.span();
     163
     164    WTF::Persistence::Decoder decoder(span);
     165   
     166    std::optional<uint32_t> version;
     167    decoder >> version;
     168    if (!version)
     169        return std::nullopt;
     170    metaData.version = WTFMove(*version);
     171
     172    std::optional<uint64_t> sourceSize;
     173    decoder >> sourceSize;
     174    if (!sourceSize)
     175        return std::nullopt;
     176    metaData.sourceSize = WTFMove(*sourceSize);
     177
     178    std::optional<uint64_t> actionsSize;
     179    decoder >> actionsSize;
     180    if (!actionsSize)
     181        return std::nullopt;
     182    metaData.actionsSize = WTFMove(*actionsSize);
     183
     184    std::optional<uint64_t> filtersWithoutConditionsBytecodeSize;
     185    decoder >> filtersWithoutConditionsBytecodeSize;
     186    if (!filtersWithoutConditionsBytecodeSize)
     187        return std::nullopt;
     188    metaData.filtersWithoutConditionsBytecodeSize = WTFMove(*filtersWithoutConditionsBytecodeSize);
     189
     190    std::optional<uint64_t> filtersWithConditionsBytecodeSize;
     191    decoder >> filtersWithConditionsBytecodeSize;
     192    if (!filtersWithConditionsBytecodeSize)
     193        return std::nullopt;
     194    metaData.filtersWithConditionsBytecodeSize = WTFMove(*filtersWithConditionsBytecodeSize);
     195
     196    std::optional<uint64_t> conditionedFiltersBytecodeSize;
     197    decoder >> conditionedFiltersBytecodeSize;
     198    if (!conditionedFiltersBytecodeSize)
     199        return std::nullopt;
     200    metaData.conditionedFiltersBytecodeSize = WTFMove(*conditionedFiltersBytecodeSize);
     201
     202    std::optional<uint32_t> conditionsApplyOnlyToDomain;
     203    decoder >> conditionsApplyOnlyToDomain;
     204    if (!conditionsApplyOnlyToDomain)
     205        return std::nullopt;
     206    metaData.conditionsApplyOnlyToDomain = WTFMove(*conditionsApplyOnlyToDomain);
     207
     208    if (metaData.version < 12)
     209        return metaData;
     210
     211    std::optional<uint64_t> frameURLFiltersBytecodeSize;
     212    decoder >> frameURLFiltersBytecodeSize;
     213    if (!frameURLFiltersBytecodeSize)
     214        return std::nullopt;
     215    metaData.frameURLFiltersBytecodeSize = WTFMove(*frameURLFiltersBytecodeSize);
     216   
     217    std::optional<uint64_t> unused;
     218    decoder >> unused;
     219    if (!unused)
     220        return std::nullopt;
     221    metaData.unused = 0;
     222
    208223    return metaData;
    209224}
     
    360375    }
    361376   
    362     char invalidHeader[ContentRuleListFileHeaderSize];
     377    char invalidHeader[CurrentVersionFileHeaderSize];
    363378    memset(invalidHeader, 0xFF, sizeof(invalidHeader));
    364379    // This header will be rewritten in CompilationClient::finalize.
     
    411426    RELEASE_ASSERT(sharedMemory);
    412427
    413     const size_t headerAndSourceSize = ContentRuleListFileHeaderSize + data.metaData.sourceSize;
     428    const size_t headerAndSourceSize = headerSize(data.metaData.version) + data.metaData.sourceSize;
    414429    auto compiledContentRuleListData = WebKit::WebCompiledContentRuleListData(
    415430        WTF::String(identifier),
    416431        sharedMemory.releaseNonNull(),
    417         ConditionsApplyOnlyToDomainOffset,
     432        data.metaData.conditionsApplyOnlyToDomain,
    418433        headerAndSourceSize,
    419434        data.metaData.actionsSize,
     
    429444            + data.metaData.filtersWithoutConditionsBytecodeSize
    430445            + data.metaData.filtersWithConditionsBytecodeSize,
    431         data.metaData.conditionedFiltersBytecodeSize
     446        data.metaData.conditionedFiltersBytecodeSize,
     447        headerAndSourceSize
     448            + data.metaData.actionsSize
     449            + data.metaData.filtersWithoutConditionsBytecodeSize
     450            + data.metaData.filtersWithConditionsBytecodeSize
     451            + data.metaData.conditionedFiltersBytecodeSize,
     452        data.metaData.frameURLFiltersBytecodeSize
    432453    );
    433454    auto compiledContentRuleList = WebKit::WebCompiledContentRuleList::create(WTFMove(compiledContentRuleListData));
     
    443464    case 10:
    444465    case 11:
     466    case 12:
    445467        if (!mappedData.metaData.sourceSize)
    446468            return { };
    447         bool is8Bit = mappedData.data.data()[ContentRuleListFileHeaderSize];
    448         size_t start = ContentRuleListFileHeaderSize + sizeof(bool);
     469           
     470        auto headerSizeBytes = headerSize(mappedData.metaData.version);
     471        bool is8Bit = mappedData.data.data()[headerSizeBytes];
     472        size_t start = headerSizeBytes + sizeof(bool);
    449473        size_t length = mappedData.metaData.sourceSize - sizeof(bool);
    450474        if (is8Bit)
  • trunk/Source/WebKit/UIProcess/API/APIContentRuleListStore.h

    r286041 r286492  
    5555    // This should be incremented every time a functional change is made to the bytecode, file format, etc.
    5656    // to prevent crashing while loading old data.
    57     // Also update ContentRuleListStore::getContentRuleListSource to be able to find the original JSON
     57    // Also update getContentRuleListSourceFromMappedFile to be able to find the original JSON
    5858    // source from old versions.
    59     // Update getContentRuleListSourceFromMappedFile with this.
    60     static constexpr uint32_t CurrentContentRuleListFileVersion = 11;
     59    static constexpr uint32_t CurrentContentRuleListFileVersion = 12;
    6160
    6261    static ContentRuleListStore& defaultStore();
  • trunk/Tools/ChangeLog

    r286488 r286492  
     12021-12-03  Alex Christensen  <achristensen@webkit.org>
     2
     3        Add room for more bytecode in WKContentRuleList file format
     4        https://bugs.webkit.org/show_bug.cgi?id=233780
     5
     6        Reviewed by Tim Hatcher.
     7
     8        * TestWebKitAPI/Tests/WebCore/ContentExtensions.cpp:
     9
    1102021-12-03  Youenn Fablet  <youenn@apple.com>
    211
  • trunk/Tools/TestWebKitAPI/Tests/WebCore/ContentExtensions.cpp

    r286402 r286492  
    6767    Vector<ContentExtensions::DFABytecode> filtersWithConditions;
    6868    Vector<ContentExtensions::DFABytecode> topURLFilters;
     69    Vector<ContentExtensions::DFABytecode> frameURLFilters;
    6970    bool conditionsApplyOnlyToDomain { false };
    7071};
     
    148149    Span<const uint8_t> filtersWithConditionsBytecode() const final { return { m_data.filtersWithConditions.data(), m_data.filtersWithConditions.size() }; }
    149150    Span<const uint8_t> topURLFiltersBytecode() const final { return { m_data.topURLFilters.data(), m_data.topURLFilters.size() }; }
     151    Span<const uint8_t> frameURLFiltersBytecode() const final { return { m_data.frameURLFilters.data(), m_data.frameURLFilters.size() }; }
    150152    bool conditionsApplyOnlyToDomain() const final { return m_data.conditionsApplyOnlyToDomain; }
    151153
     
    967969    ASSERT_EQ(sequenceInstances(data.actions, "GGG"), 1);
    968970
    969     ASSERT_EQ(data.actions.size(), 78u);
     971    ASSERT_EQ(data.actions.size(), 72u);
    970972    ASSERT_EQ(data.filtersWithoutConditions.size(), 288u);
    971973    ASSERT_EQ(data.filtersWithConditions.size(), 5u);
  • trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKContentExtensionStore.mm

    r286383 r286492  
    253253    NSData *data = [NSData dataWithContentsOfURL:[tempDir URLByAppendingPathComponent:fileName]];
    254254    EXPECT_NOT_NULL(data);
    255     EXPECT_EQ(data.length, 225u);
     255    EXPECT_EQ(data.length, 241u);
    256256   
    257257    __block bool doneCheckingSource = false;
Note: See TracChangeset for help on using the changeset viewer.