⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 242860 in webkit


Ignore:
Timestamp:
Mar 13, 2019, 1:24:51 AM (7 years ago)
Author:
bshafiei@apple.com
Message:

Cherry-pick r242735. rdar://problem/48780112

Add a WKContentRuleList variant that uses copied memory instead of mmap'd shared memory for class A containerized apps
https://bugs.webkit.org/show_bug.cgi?id=195511
<rdar://problem/44873269>

Patch by Alex Christensen <achristensen@webkit.org> on 2019-03-11
Reviewed by Darin Adler.

Source/WebKit:

  • NetworkProcess/NetworkContentRuleListManager.cpp: (WebKit::NetworkContentRuleListManager::addContentRuleLists):
  • NetworkProcess/NetworkContentRuleListManager.h:
  • NetworkProcess/cache/NetworkCacheFileSystem.cpp: (WebKit::NetworkCache::pathRegisteredAsUnsafeToMemoryMapForTesting): (WebKit::NetworkCache::registerPathAsUnsafeToMemoryMapForTesting): (WebKit::NetworkCache::isSafeToUseMemoryMapForPath):
  • NetworkProcess/cache/NetworkCacheFileSystem.h:
  • Shared/WebCompiledContentRuleList.cpp: (WebKit::WebCompiledContentRuleList::usesCopiedMemory const): (WebKit::WebCompiledContentRuleList::conditionsApplyOnlyToDomain const): (WebKit::WebCompiledContentRuleList::filtersWithoutConditionsBytecode const): (WebKit::WebCompiledContentRuleList::filtersWithConditionsBytecode const): (WebKit::WebCompiledContentRuleList::topURLFiltersBytecode const): (WebKit::WebCompiledContentRuleList::actions const):
  • Shared/WebCompiledContentRuleList.h:
  • Shared/WebCompiledContentRuleListData.cpp: (WebKit::WebCompiledContentRuleListData::size const): (WebKit::WebCompiledContentRuleListData::dataPointer const): (WebKit::WebCompiledContentRuleListData::encode const): (WebKit::WebCompiledContentRuleListData::decode):
  • Shared/WebCompiledContentRuleListData.h: (WebKit::WebCompiledContentRuleListData::WebCompiledContentRuleListData):
  • UIProcess/API/APIContentRuleList.cpp: (API::ContentRuleList::usesCopiedMemory const):
  • UIProcess/API/APIContentRuleList.h:
  • UIProcess/API/APIContentRuleListStore.cpp: (API::getData): (API::decodeContentRuleListMetaData): (API::ContentRuleListStore::readContentsOfFile): (API::MappedOrCopiedData::dataPointer const): (API::openAndMapOrCopyContentRuleList): (API::compiledToFile): (API::createExtension): (API::ContentRuleListStore::lookupContentRuleList): (API::ContentRuleListStore::compileContentRuleList): (API::ContentRuleListStore::getContentRuleListSource): (API::openAndMapContentRuleList): Deleted.
  • UIProcess/API/APIContentRuleListStore.h:
  • UIProcess/API/Cocoa/APIContentRuleListStoreCocoa.mm: (API::ContentRuleListStore::readContentsOfFile):
  • UIProcess/API/Cocoa/WKContentRuleListStore.mm: (+[WKContentRuleListStore _registerPathAsUnsafeToMemoryMapForTesting:]):
  • UIProcess/API/Cocoa/WKContentRuleListStorePrivate.h:
  • UIProcess/API/Cocoa/_WKUserContentFilter.mm: (-[_WKUserContentFilter usesCopiedMemory]):
  • UIProcess/API/Cocoa/_WKUserContentFilterPrivate.h:
  • WebProcess/UserContent/WebUserContentController.cpp: (WebKit::WebUserContentController::addContentRuleLists):
  • WebProcess/UserContent/WebUserContentController.h:
  • WebProcess/WebPage/WebPage.cpp: (WebKit::m_hostFileDescriptor):

Tools:

  • TestWebKitAPI/Tests/WebKitCocoa/WKContentExtensionStore.mm: (-[TestSchemeHandlerSubresourceShouldBeBlocked webView:startURLSchemeTask:]): (-[TestSchemeHandlerSubresourceShouldBeBlocked webView:stopURLSchemeTask:]): (TEST_F):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@242735 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Location:
branches/safari-607-branch
Files:
23 edited

Legend:

Unmodified
Added
Removed
  • branches/safari-607-branch/Source/WebKit/ChangeLog

    r242858 r242860  
     12019-03-13  Babak Shafiei  <bshafiei@apple.com>
     2
     3        Cherry-pick r242735. rdar://problem/48780112
     4
     5    Add a WKContentRuleList variant that uses copied memory instead of mmap'd shared memory for class A containerized apps
     6    https://bugs.webkit.org/show_bug.cgi?id=195511
     7    <rdar://problem/44873269>
     8   
     9    Patch by Alex Christensen <achristensen@webkit.org> on 2019-03-11
     10    Reviewed by Darin Adler.
     11   
     12    Source/WebKit:
     13   
     14    * NetworkProcess/NetworkContentRuleListManager.cpp:
     15    (WebKit::NetworkContentRuleListManager::addContentRuleLists):
     16    * NetworkProcess/NetworkContentRuleListManager.h:
     17    * NetworkProcess/cache/NetworkCacheFileSystem.cpp:
     18    (WebKit::NetworkCache::pathRegisteredAsUnsafeToMemoryMapForTesting):
     19    (WebKit::NetworkCache::registerPathAsUnsafeToMemoryMapForTesting):
     20    (WebKit::NetworkCache::isSafeToUseMemoryMapForPath):
     21    * NetworkProcess/cache/NetworkCacheFileSystem.h:
     22    * Shared/WebCompiledContentRuleList.cpp:
     23    (WebKit::WebCompiledContentRuleList::usesCopiedMemory const):
     24    (WebKit::WebCompiledContentRuleList::conditionsApplyOnlyToDomain const):
     25    (WebKit::WebCompiledContentRuleList::filtersWithoutConditionsBytecode const):
     26    (WebKit::WebCompiledContentRuleList::filtersWithConditionsBytecode const):
     27    (WebKit::WebCompiledContentRuleList::topURLFiltersBytecode const):
     28    (WebKit::WebCompiledContentRuleList::actions const):
     29    * Shared/WebCompiledContentRuleList.h:
     30    * Shared/WebCompiledContentRuleListData.cpp:
     31    (WebKit::WebCompiledContentRuleListData::size const):
     32    (WebKit::WebCompiledContentRuleListData::dataPointer const):
     33    (WebKit::WebCompiledContentRuleListData::encode const):
     34    (WebKit::WebCompiledContentRuleListData::decode):
     35    * Shared/WebCompiledContentRuleListData.h:
     36    (WebKit::WebCompiledContentRuleListData::WebCompiledContentRuleListData):
     37    * UIProcess/API/APIContentRuleList.cpp:
     38    (API::ContentRuleList::usesCopiedMemory const):
     39    * UIProcess/API/APIContentRuleList.h:
     40    * UIProcess/API/APIContentRuleListStore.cpp:
     41    (API::getData):
     42    (API::decodeContentRuleListMetaData):
     43    (API::ContentRuleListStore::readContentsOfFile):
     44    (API::MappedOrCopiedData::dataPointer const):
     45    (API::openAndMapOrCopyContentRuleList):
     46    (API::compiledToFile):
     47    (API::createExtension):
     48    (API::ContentRuleListStore::lookupContentRuleList):
     49    (API::ContentRuleListStore::compileContentRuleList):
     50    (API::ContentRuleListStore::getContentRuleListSource):
     51    (API::openAndMapContentRuleList): Deleted.
     52    * UIProcess/API/APIContentRuleListStore.h:
     53    * UIProcess/API/Cocoa/APIContentRuleListStoreCocoa.mm:
     54    (API::ContentRuleListStore::readContentsOfFile):
     55    * UIProcess/API/Cocoa/WKContentRuleListStore.mm:
     56    (+[WKContentRuleListStore _registerPathAsUnsafeToMemoryMapForTesting:]):
     57    * UIProcess/API/Cocoa/WKContentRuleListStorePrivate.h:
     58    * UIProcess/API/Cocoa/_WKUserContentFilter.mm:
     59    (-[_WKUserContentFilter usesCopiedMemory]):
     60    * UIProcess/API/Cocoa/_WKUserContentFilterPrivate.h:
     61    * WebProcess/UserContent/WebUserContentController.cpp:
     62    (WebKit::WebUserContentController::addContentRuleLists):
     63    * WebProcess/UserContent/WebUserContentController.h:
     64    * WebProcess/WebPage/WebPage.cpp:
     65    (WebKit::m_hostFileDescriptor):
     66   
     67    Tools:
     68   
     69    * TestWebKitAPI/Tests/WebKitCocoa/WKContentExtensionStore.mm:
     70    (-[TestSchemeHandlerSubresourceShouldBeBlocked webView:startURLSchemeTask:]):
     71    (-[TestSchemeHandlerSubresourceShouldBeBlocked webView:stopURLSchemeTask:]):
     72    (TEST_F):
     73   
     74    git-svn-id: https://svn.webkit.org/repository/webkit/trunk@242735 268f45cc-cd09-0410-ab3c-d52691b4dbfc
     75
     76    2019-03-11  Alex Christensen  <achristensen@webkit.org>
     77
     78            Add a WKContentRuleList variant that uses copied memory instead of mmap'd shared memory for class A containerized apps
     79            https://bugs.webkit.org/show_bug.cgi?id=195511
     80            <rdar://problem/44873269>
     81
     82            Reviewed by Darin Adler.
     83
     84            * NetworkProcess/NetworkContentRuleListManager.cpp:
     85            (WebKit::NetworkContentRuleListManager::addContentRuleLists):
     86            * NetworkProcess/NetworkContentRuleListManager.h:
     87            * NetworkProcess/cache/NetworkCacheFileSystem.cpp:
     88            (WebKit::NetworkCache::pathRegisteredAsUnsafeToMemoryMapForTesting):
     89            (WebKit::NetworkCache::registerPathAsUnsafeToMemoryMapForTesting):
     90            (WebKit::NetworkCache::isSafeToUseMemoryMapForPath):
     91            * NetworkProcess/cache/NetworkCacheFileSystem.h:
     92            * Shared/WebCompiledContentRuleList.cpp:
     93            (WebKit::WebCompiledContentRuleList::usesCopiedMemory const):
     94            (WebKit::WebCompiledContentRuleList::conditionsApplyOnlyToDomain const):
     95            (WebKit::WebCompiledContentRuleList::filtersWithoutConditionsBytecode const):
     96            (WebKit::WebCompiledContentRuleList::filtersWithConditionsBytecode const):
     97            (WebKit::WebCompiledContentRuleList::topURLFiltersBytecode const):
     98            (WebKit::WebCompiledContentRuleList::actions const):
     99            * Shared/WebCompiledContentRuleList.h:
     100            * Shared/WebCompiledContentRuleListData.cpp:
     101            (WebKit::WebCompiledContentRuleListData::size const):
     102            (WebKit::WebCompiledContentRuleListData::dataPointer const):
     103            (WebKit::WebCompiledContentRuleListData::encode const):
     104            (WebKit::WebCompiledContentRuleListData::decode):
     105            * Shared/WebCompiledContentRuleListData.h:
     106            (WebKit::WebCompiledContentRuleListData::WebCompiledContentRuleListData):
     107            * UIProcess/API/APIContentRuleList.cpp:
     108            (API::ContentRuleList::usesCopiedMemory const):
     109            * UIProcess/API/APIContentRuleList.h:
     110            * UIProcess/API/APIContentRuleListStore.cpp:
     111            (API::getData):
     112            (API::decodeContentRuleListMetaData):
     113            (API::ContentRuleListStore::readContentsOfFile):
     114            (API::MappedOrCopiedData::dataPointer const):
     115            (API::openAndMapOrCopyContentRuleList):
     116            (API::compiledToFile):
     117            (API::createExtension):
     118            (API::ContentRuleListStore::lookupContentRuleList):
     119            (API::ContentRuleListStore::compileContentRuleList):
     120            (API::ContentRuleListStore::getContentRuleListSource):
     121            (API::openAndMapContentRuleList): Deleted.
     122            * UIProcess/API/APIContentRuleListStore.h:
     123            * UIProcess/API/Cocoa/APIContentRuleListStoreCocoa.mm:
     124            (API::ContentRuleListStore::readContentsOfFile):
     125            * UIProcess/API/Cocoa/WKContentRuleListStore.mm:
     126            (+[WKContentRuleListStore _registerPathAsUnsafeToMemoryMapForTesting:]):
     127            * UIProcess/API/Cocoa/WKContentRuleListStorePrivate.h:
     128            * UIProcess/API/Cocoa/_WKUserContentFilter.mm:
     129            (-[_WKUserContentFilter usesCopiedMemory]):
     130            * UIProcess/API/Cocoa/_WKUserContentFilterPrivate.h:
     131            * WebProcess/UserContent/WebUserContentController.cpp:
     132            (WebKit::WebUserContentController::addContentRuleLists):
     133            * WebProcess/UserContent/WebUserContentController.h:
     134            * WebProcess/WebPage/WebPage.cpp:
     135            (WebKit::m_hostFileDescriptor):
     136
    11372019-03-13  Babak Shafiei  <bshafiei@apple.com>
    2138
  • branches/safari-607-branch/Source/WebKit/NetworkProcess/NetworkContentRuleListManager.cpp

    r235101 r242860  
    6363}
    6464
    65 void NetworkContentRuleListManager::addContentRuleLists(UserContentControllerIdentifier identifier, const Vector<std::pair<String, WebCompiledContentRuleListData>>& contentRuleLists)
     65void NetworkContentRuleListManager::addContentRuleLists(UserContentControllerIdentifier identifier, Vector<std::pair<String, WebCompiledContentRuleListData>>&& contentRuleLists)
    6666{
    6767    auto& backend = *m_contentExtensionBackends.ensure(identifier, [] {
     
    6969    }).iterator->value;
    7070
    71     for (const auto& contentRuleList : contentRuleLists) {
    72         WebCompiledContentRuleListData contentRuleListData = contentRuleList.second;
    73         auto compiledContentRuleList = WebCompiledContentRuleList::create(WTFMove(contentRuleListData));
     71    for (auto&& contentRuleList : contentRuleLists) {
     72        auto compiledContentRuleList = WebCompiledContentRuleList::create(WTFMove(contentRuleList.second));
    7473        backend.addContentExtension(contentRuleList.first, WTFMove(compiledContentRuleList), ContentExtensions::ContentExtension::ShouldCompileCSS::No);
    7574    }
  • branches/safari-607-branch/Source/WebKit/NetworkProcess/NetworkContentRuleListManager.h

    r239007 r242860  
    5050
    5151private:
    52     void addContentRuleLists(UserContentControllerIdentifier, const Vector<std::pair<String, WebCompiledContentRuleListData>>&);
     52    void addContentRuleLists(UserContentControllerIdentifier, Vector<std::pair<String, WebCompiledContentRuleListData>>&&);
    5353    void removeContentRuleList(UserContentControllerIdentifier, const String& name);
    5454    void removeAllContentRuleLists(UserContentControllerIdentifier);
  • branches/safari-607-branch/Source/WebKit/NetworkProcess/cache/NetworkCacheFileSystem.cpp

    r239366 r242860  
    146146}
    147147
     148static String& pathRegisteredAsUnsafeToMemoryMapForTesting()
     149{
     150    static NeverDestroyed<String> path;
     151    return path.get();
     152}
     153
     154void registerPathAsUnsafeToMemoryMapForTesting(const String& path)
     155{
     156    pathRegisteredAsUnsafeToMemoryMapForTesting() = path;
     157}
     158
     159   
    148160bool isSafeToUseMemoryMapForPath(const String& path)
    149161{
     162    if (path == pathRegisteredAsUnsafeToMemoryMapForTesting())
     163        return false;
     164
    150165#if PLATFORM(IOS_FAMILY) && !PLATFORM(IOS_FAMILY_SIMULATOR)
    151166    struct {
  • branches/safari-607-branch/Source/WebKit/NetworkProcess/cache/NetworkCacheFileSystem.h

    r231484 r242860  
    4444
    4545bool isSafeToUseMemoryMapForPath(const String& path);
     46void registerPathAsUnsafeToMemoryMapForTesting(const String&);
    4647
    4748}
  • branches/safari-607-branch/Source/WebKit/Shared/WebCompiledContentRuleList.cpp

    r216809 r242860  
    4545}
    4646
     47bool WebCompiledContentRuleList::usesCopiedMemory() const
     48{
     49    return WTF::holds_alternative<RefPtr<WebCore::SharedBuffer>>(m_data.data);
     50}
     51
    4752bool WebCompiledContentRuleList::conditionsApplyOnlyToDomain() const
    4853{
    49     return *reinterpret_cast<uint32_t*>(reinterpret_cast<uint8_t*>(m_data.data->data()) + m_data.conditionsApplyOnlyToDomainOffset);
     54    return *reinterpret_cast<const uint32_t*>(reinterpret_cast<const uint8_t*>(m_data.dataPointer()) + m_data.conditionsApplyOnlyToDomainOffset);
    5055}
    5156
    5257const WebCore::ContentExtensions::DFABytecode* WebCompiledContentRuleList::filtersWithoutConditionsBytecode() const
    5358{
    54     return static_cast<const WebCore::ContentExtensions::DFABytecode*>(m_data.data->data()) + m_data.filtersWithoutConditionsBytecodeOffset;
     59    return static_cast<const WebCore::ContentExtensions::DFABytecode*>(m_data.dataPointer()) + m_data.filtersWithoutConditionsBytecodeOffset;
    5560}
    5661
     
    6267const WebCore::ContentExtensions::DFABytecode* WebCompiledContentRuleList::filtersWithConditionsBytecode() const
    6368{
    64     return static_cast<const WebCore::ContentExtensions::DFABytecode*>(m_data.data->data()) + m_data.filtersWithConditionsBytecodeOffset;
     69    return static_cast<const WebCore::ContentExtensions::DFABytecode*>(m_data.dataPointer()) + m_data.filtersWithConditionsBytecodeOffset;
    6570}
    6671
     
    7277const WebCore::ContentExtensions::DFABytecode* WebCompiledContentRuleList::topURLFiltersBytecode() const
    7378{
    74     return static_cast<const WebCore::ContentExtensions::DFABytecode*>(m_data.data->data()) + m_data.topURLFiltersBytecodeOffset;
     79    return static_cast<const WebCore::ContentExtensions::DFABytecode*>(m_data.dataPointer()) + m_data.topURLFiltersBytecodeOffset;
    7580}
    7681
     
    8287const WebCore::ContentExtensions::SerializedActionByte* WebCompiledContentRuleList::actions() const
    8388{
    84     return static_cast<const WebCore::ContentExtensions::SerializedActionByte*>(m_data.data->data()) + m_data.actionsOffset;
     89    return static_cast<const WebCore::ContentExtensions::SerializedActionByte*>(m_data.dataPointer()) + m_data.actionsOffset;
    8590}
    8691
  • branches/safari-607-branch/Source/WebKit/Shared/WebCompiledContentRuleList.h

    r216809 r242860  
    3939    virtual ~WebCompiledContentRuleList();
    4040
    41     WebCompiledContentRuleListData data() const { return m_data; }
     41    const WebCompiledContentRuleListData& data() const { return m_data; }
    4242
     43    bool usesCopiedMemory() const;
     44   
    4345private:
    4446    WebCompiledContentRuleList(WebCompiledContentRuleListData&&);
  • branches/safari-607-branch/Source/WebKit/Shared/WebCompiledContentRuleListData.cpp

    r239427 r242860  
    3030
    3131#include "ArgumentCoders.h"
     32#include "SharedBufferDataReference.h"
    3233
    3334namespace WebKit {
    3435
     36size_t WebCompiledContentRuleListData::size() const
     37{
     38    return WTF::switchOn(data, [] (const auto& sharedMemoryOrBuffer) {
     39        return sharedMemoryOrBuffer->size();
     40    });
     41}
     42
     43const void* WebCompiledContentRuleListData::dataPointer() const
     44{
     45    return WTF::switchOn(data, [] (const auto& sharedMemoryOrBuffer) -> const void* {
     46        return sharedMemoryOrBuffer->data();
     47    });
     48}
     49
    3550void WebCompiledContentRuleListData::encode(IPC::Encoder& encoder) const
    3651{
    37     SharedMemory::Handle handle;
    38     data->createHandle(handle, SharedMemory::Protection::ReadOnly);
    39     encoder << handle;
     52    if (auto sharedMemory = WTF::get_if<RefPtr<SharedMemory>>(data)) {
     53        encoder << true;
     54        SharedMemory::Handle handle;
     55        sharedMemory->get()->createHandle(handle, SharedMemory::Protection::ReadOnly);
     56        encoder << handle;
     57    } else {
     58        encoder << false;
     59        encoder << IPC::SharedBufferDataReference { *WTF::get<RefPtr<WebCore::SharedBuffer>>(data) };
     60    }
     61
     62    // fileData needs to be kept in the UIProcess, but it does not need to be serialized.
     63    // FIXME: Move it to API::ContentRuleList
    4064
    4165    encoder << conditionsApplyOnlyToDomainOffset;
     
    5377{
    5478    WebCompiledContentRuleListData compiledContentRuleListData;
    55     SharedMemory::Handle handle;
    56     if (!decoder.decode(handle))
     79
     80    Optional<bool> hasSharedMemory;
     81    decoder >> hasSharedMemory;
     82    if (!hasSharedMemory)
    5783        return WTF::nullopt;
    58     compiledContentRuleListData.data = SharedMemory::map(handle, SharedMemory::Protection::ReadOnly);
     84    if (*hasSharedMemory) {
     85        SharedMemory::Handle handle;
     86        if (!decoder.decode(handle))
     87            return WTF::nullopt;
     88        compiledContentRuleListData.data = { SharedMemory::map(handle, SharedMemory::Protection::ReadOnly) };
     89    } else {
     90        IPC::DataReference dataReference;
     91        if (!decoder.decode(dataReference))
     92            return WTF::nullopt;
     93        compiledContentRuleListData.data = { RefPtr<WebCore::SharedBuffer>(WebCore::SharedBuffer::create(dataReference.data(), dataReference.size())) };
     94    }
    5995
    6096    if (!decoder.decode(compiledContentRuleListData.conditionsApplyOnlyToDomainOffset))
  • branches/safari-607-branch/Source/WebKit/Shared/WebCompiledContentRuleListData.h

    r239427 r242860  
    3030#include "NetworkCacheData.h"
    3131#include "SharedMemory.h"
     32#include <WebCore/SharedBuffer.h>
    3233#include <wtf/RefPtr.h>
     34#include <wtf/Variant.h>
    3335
    3436namespace IPC {
     
    4345    WebCompiledContentRuleListData() = default;
    4446
    45     WebCompiledContentRuleListData(RefPtr<SharedMemory>&& data, NetworkCache::Data fileData, unsigned conditionsApplyOnlyToDomainOffset, unsigned actionsOffset, unsigned actionsSize, unsigned filtersWithoutConditionsBytecodeOffset, unsigned filtersWithoutConditionsBytecodeSize, unsigned filtersWithConditionsBytecodeOffset, unsigned filtersWithConditionsBytecodeSize, unsigned topURLFiltersBytecodeOffset, unsigned topURLFiltersBytecodeSize)
     47    WebCompiledContentRuleListData(Variant<RefPtr<SharedMemory>, RefPtr<WebCore::SharedBuffer>>&& data, NetworkCache::Data fileData, unsigned conditionsApplyOnlyToDomainOffset, unsigned actionsOffset, unsigned actionsSize, unsigned filtersWithoutConditionsBytecodeOffset, unsigned filtersWithoutConditionsBytecodeSize, unsigned filtersWithConditionsBytecodeOffset, unsigned filtersWithConditionsBytecodeSize, unsigned topURLFiltersBytecodeOffset, unsigned topURLFiltersBytecodeSize)
    4648        : data(WTFMove(data))
    4749        , fileData(fileData)
     
    6163    static Optional<WebCompiledContentRuleListData> decode(IPC::Decoder&);
    6264
    63     RefPtr<SharedMemory> data;
     65    size_t size() const;
     66    const void* dataPointer() const;
     67   
     68    Variant<RefPtr<SharedMemory>, RefPtr<WebCore::SharedBuffer>> data;
    6469    NetworkCache::Data fileData;
    6570    unsigned conditionsApplyOnlyToDomainOffset { 0 };
  • branches/safari-607-branch/Source/WebKit/UIProcess/API/APIContentRuleList.cpp

    r216809 r242860  
    4343}
    4444
     45bool ContentRuleList::usesCopiedMemory() const
     46{
     47    return m_compiledRuleList->usesCopiedMemory();
     48}
     49
    4550} // namespace API
    4651
  • branches/safari-607-branch/Source/WebKit/UIProcess/API/APIContentRuleList.h

    r216809 r242860  
    4949    const WebKit::WebCompiledContentRuleList& compiledRuleList() const { return m_compiledRuleList.get(); }
    5050
     51    bool usesCopiedMemory() const;
     52
    5153private:
    5254    WTF::String m_name;
  • branches/safari-607-branch/Source/WebKit/UIProcess/API/APIContentRuleListStore.cpp

    r239260 r242860  
    3838#include <WebCore/ContentExtensionParser.h>
    3939#include <WebCore/QualifiedName.h>
     40#include <WebCore/SharedBuffer.h>
    4041#include <string>
    4142#include <wtf/CompletionHandler.h>
     
    156157}
    157158
    158 static bool decodeContentRuleListMetaData(ContentRuleListMetaData& metaData, const WebKit::NetworkCache::Data& fileData)
     159template<typename T> void getData(const T&, const Function<bool(const uint8_t*, size_t)>&);
     160template<> void getData(const WebKit::NetworkCache::Data& data, const Function<bool(const uint8_t*, size_t)>& function)
     161{
     162    data.apply(function);
     163}
     164template<> void getData(const WebCore::SharedBuffer& data, const Function<bool(const uint8_t*, size_t)>& function)
     165{
     166    function(reinterpret_cast<const uint8_t*>(data.data()), data.size());
     167}
     168
     169template<typename T>
     170static Optional<ContentRuleListMetaData> decodeContentRuleListMetaData(const T& fileData)
    159171{
    160172    bool success = false;
    161     fileData.apply([&metaData, &success, &fileData](const uint8_t* data, size_t size) {
     173    ContentRuleListMetaData metaData;
     174    getData(fileData, [&metaData, &success, &fileData](const uint8_t* data, size_t size) {
    162175        // The file data should be mapped into one continuous memory segment so the size
    163176        // passed to the applier should always equal the data size.
     
    183196        return false;
    184197    });
    185     return success;
    186 }
    187 
    188 static bool openAndMapContentRuleList(const WTF::String& path, ContentRuleListMetaData& metaData, WebKit::NetworkCache::Data& fileData)
    189 {
    190     fileData = mapFile(fileSystemRepresentation(path).data());
     198    if (!success)
     199        return WTF::nullopt;
     200    return WTFMove(metaData);
     201}
     202
     203#if !PLATFORM(COCOA)
     204RefPtr<WebCore::SharedBuffer> ContentRuleListStore::readContentsOfFile(const WTF::String& filePath)
     205{
     206    ASSERT_NOT_REACHED();
     207    return nullptr;
     208}
     209#endif
     210
     211struct MappedOrCopiedData {
     212    ContentRuleListMetaData metaData;
     213    Variant<WebKit::NetworkCache::Data, RefPtr<WebCore::SharedBuffer>> data;
     214   
     215    const uint8_t* dataPointer() const
     216    {
     217        return WTF::switchOn(data, [] (const WebKit::NetworkCache::Data& data) {
     218            return data.data();
     219        }, [] (const RefPtr<WebCore::SharedBuffer>& sharedBuffer) {
     220            return reinterpret_cast<const uint8_t*>(sharedBuffer->data());
     221        });
     222    }
     223};
     224
     225static Optional<MappedOrCopiedData> openAndMapOrCopyContentRuleList(const WTF::String& path)
     226{
     227    if (!WebKit::NetworkCache::isSafeToUseMemoryMapForPath(path)) {
     228        RefPtr<WebCore::SharedBuffer> buffer = ContentRuleListStore::readContentsOfFile(path);
     229        if (!buffer)
     230            return WTF::nullopt;
     231        auto metaData = decodeContentRuleListMetaData(*buffer);
     232        if (!metaData)
     233            return WTF::nullopt;
     234        return {{ WTFMove(*metaData), { buffer.releaseNonNull() }}};
     235    }
     236
     237    WebKit::NetworkCache::Data fileData = mapFile(fileSystemRepresentation(path).data());
    191238    if (fileData.isNull())
    192         return false;
    193 
    194     if (!decodeContentRuleListMetaData(metaData, fileData))
    195         return false;
    196 
    197     return true;
     239        return WTF::nullopt;
     240    auto metaData = decodeContentRuleListMetaData(fileData);
     241    if (!metaData)
     242        return WTF::nullopt;
     243    return {{ WTFMove(*metaData), { WTFMove(fileData) }}};
    198244}
    199245
     
    212258}
    213259
    214 static std::error_code compiledToFile(WTF::String&& json, Vector<WebCore::ContentExtensions::ContentExtensionRule>&& parsedRules, const WTF::String& finalFilePath, ContentRuleListMetaData& metaData, WebKit::NetworkCache::Data& mappedData)
     260static Expected<MappedOrCopiedData, std::error_code> compiledToFile(WTF::String&& json, Vector<WebCore::ContentExtensions::ContentExtensionRule>&& parsedRules, const WTF::String& finalFilePath)
    215261{
    216262    using namespace WebCore::ContentExtensions;
     
    329375    if (temporaryFileHandle == invalidPlatformFileHandle) {
    330376        WTFLogAlways("Content Rule List compiling failed: Opening temporary file failed.");
    331         return ContentRuleListStore::Error::CompileFailed;
     377        return makeUnexpected(ContentRuleListStore::Error::CompileFailed);
    332378    }
    333379   
     
    338384        WTFLogAlways("Content Rule List compiling failed: Writing header to file failed.");
    339385        closeFile(temporaryFileHandle);
    340         return ContentRuleListStore::Error::CompileFailed;
    341     }
    342 
     386        return makeUnexpected(ContentRuleListStore::Error::CompileFailed);
     387    }
     388
     389    ContentRuleListMetaData metaData;
    343390    CompilationClient compilationClient(temporaryFileHandle, metaData);
    344391   
     
    346393        WTFLogAlways("Content Rule List compiling failed: Compiling failed.");
    347394        closeFile(temporaryFileHandle);
    348         return compilerError;
     395        return makeUnexpected(compilerError);
    349396    }
    350397    if (compilationClient.hadErrorWhileWritingToFile()) {
    351398        WTFLogAlways("Content Rule List compiling failed: Writing to file failed.");
    352399        closeFile(temporaryFileHandle);
    353         return ContentRuleListStore::Error::CompileFailed;
    354     }
    355 
    356     mappedData = adoptAndMapFile(temporaryFileHandle, 0, metaData.fileSize());
     400        return makeUnexpected(ContentRuleListStore::Error::CompileFailed);
     401    }
     402
     403    auto mappedData = adoptAndMapFile(temporaryFileHandle, 0, metaData.fileSize());
    357404    if (mappedData.isNull()) {
    358405        WTFLogAlways("Content Rule List compiling failed: Mapping file failed.");
    359         return ContentRuleListStore::Error::CompileFailed;
     406        return makeUnexpected(ContentRuleListStore::Error::CompileFailed);
    360407    }
    361408
    362409    if (!moveFile(temporaryFilePath, finalFilePath)) {
    363410        WTFLogAlways("Content Rule List compiling failed: Moving file failed.");
    364         return ContentRuleListStore::Error::CompileFailed;
    365     }
    366 
    367     return { };
    368 }
    369 
    370 static Ref<API::ContentRuleList> createExtension(const WTF::String& identifier, const ContentRuleListMetaData& metaData, const WebKit::NetworkCache::Data& fileData)
    371 {
    372     // Content extensions are always compiled to files, and at this point the file
    373     // has been already mapped, therefore tryCreateSharedMemory() cannot fail.
    374     auto sharedMemory = fileData.tryCreateSharedMemory();
    375     ASSERT(sharedMemory);
    376 
    377     const size_t headerAndSourceSize = ContentRuleListFileHeaderSize + metaData.sourceSize;
     411        return makeUnexpected(ContentRuleListStore::Error::CompileFailed);
     412    }
     413
     414    if (!isSafeToUseMemoryMapForPath(finalFilePath)) {
     415        auto contents = ContentRuleListStore::readContentsOfFile(finalFilePath);
     416        if (!contents)
     417            return makeUnexpected(ContentRuleListStore::Error::CompileFailed);
     418        return {{ WTFMove(metaData), WTFMove(contents) }};
     419    }
     420   
     421    return {{ WTFMove(metaData), WTFMove(mappedData) }};
     422}
     423
     424static Ref<API::ContentRuleList> createExtension(const WTF::String& identifier, MappedOrCopiedData&& data)
     425{
     426    RefPtr<WebKit::SharedMemory> sharedMemory;
     427    if (auto mappedFileData = WTF::get_if<WebKit::NetworkCache::Data>(data.data)) {
     428        sharedMemory = mappedFileData->tryCreateSharedMemory();
     429
     430        // Content extensions are always compiled to files, and at this point the file
     431        // has been already mapped, therefore tryCreateSharedMemory() cannot fail.
     432        ASSERT(sharedMemory);
     433    }
     434    auto mappedOrCopiedFileData = sharedMemory ?
     435        Variant<RefPtr<WebKit::SharedMemory>, RefPtr<WebCore::SharedBuffer>> { sharedMemory }
     436        : Variant<RefPtr<WebKit::SharedMemory>, RefPtr<WebCore::SharedBuffer>> { WTFMove(WTF::get<RefPtr<WebCore::SharedBuffer>>(data.data)) };
     437
     438    const size_t headerAndSourceSize = ContentRuleListFileHeaderSize + data.metaData.sourceSize;
    378439    auto compiledContentRuleListData = WebKit::WebCompiledContentRuleListData(
    379         WTFMove(sharedMemory),
    380         fileData,
     440        WTFMove(mappedOrCopiedFileData),
     441        WTF::holds_alternative<WebKit::NetworkCache::Data>(data.data) ? WTF::get<WebKit::NetworkCache::Data>(data.data) : WebKit::NetworkCache::Data { },
    381442        ConditionsApplyOnlyToDomainOffset,
    382443        headerAndSourceSize,
    383         metaData.actionsSize,
     444        data.metaData.actionsSize,
    384445        headerAndSourceSize
    385             + metaData.actionsSize,
    386         metaData.filtersWithoutConditionsBytecodeSize,
     446            + data.metaData.actionsSize,
     447        data.metaData.filtersWithoutConditionsBytecodeSize,
    387448        headerAndSourceSize
    388             + metaData.actionsSize
    389             + metaData.filtersWithoutConditionsBytecodeSize,
    390         metaData.filtersWithConditionsBytecodeSize,
     449            + data.metaData.actionsSize
     450            + data.metaData.filtersWithoutConditionsBytecodeSize,
     451        data.metaData.filtersWithConditionsBytecodeSize,
    391452        headerAndSourceSize
    392             + metaData.actionsSize
    393             + metaData.filtersWithoutConditionsBytecodeSize
    394             + metaData.filtersWithConditionsBytecodeSize,
    395         metaData.conditionedFiltersBytecodeSize
     453            + data.metaData.actionsSize
     454            + data.metaData.filtersWithoutConditionsBytecodeSize
     455            + data.metaData.filtersWithConditionsBytecodeSize,
     456        data.metaData.conditionedFiltersBytecodeSize
    396457    );
    397458    auto compiledContentRuleList = WebKit::WebCompiledContentRuleList::create(WTFMove(compiledContentRuleListData));
     
    404465        auto path = constructedPath(storePath, identifier, legacyFilename);
    405466       
    406         ContentRuleListMetaData metaData;
    407         WebKit::NetworkCache::Data fileData;
    408         if (!openAndMapContentRuleList(path, metaData, fileData)) {
     467        auto contentRuleList = openAndMapOrCopyContentRuleList(path);
     468        if (!contentRuleList) {
    409469            RunLoop::main().dispatch([protectedThis = WTFMove(protectedThis), completionHandler = WTFMove(completionHandler)] () mutable {
    410470                completionHandler(nullptr, Error::LookupFailed);
     
    413473        }
    414474       
    415         if (metaData.version != ContentRuleListStore::CurrentContentRuleListFileVersion) {
     475        if (contentRuleList->metaData.version != ContentRuleListStore::CurrentContentRuleListFileVersion) {
    416476            RunLoop::main().dispatch([protectedThis = WTFMove(protectedThis), completionHandler = WTFMove(completionHandler)] () mutable {
    417477                completionHandler(nullptr, Error::VersionMismatch);
     
    420480        }
    421481       
    422         RunLoop::main().dispatch([protectedThis = WTFMove(protectedThis), identifier = identifier.isolatedCopy(), fileData = WTFMove(fileData), metaData = WTFMove(metaData), completionHandler = WTFMove(completionHandler)] () mutable {
    423             completionHandler(createExtension(identifier, metaData, fileData), { });
     482        RunLoop::main().dispatch([protectedThis = WTFMove(protectedThis), identifier = identifier.isolatedCopy(), contentRuleList = WTFMove(*contentRuleList), completionHandler = WTFMove(completionHandler)] () mutable {
     483            completionHandler(createExtension(identifier, WTFMove(contentRuleList)), { });
    424484        });
    425485    });
     
    455515        auto path = constructedPath(storePath, identifier, legacyFilename);
    456516
    457         ContentRuleListMetaData metaData;
    458         WebKit::NetworkCache::Data fileData;
    459         auto error = compiledToFile(WTFMove(json), WTFMove(parsedRules), path, metaData, fileData);
    460         if (error) {
    461             RunLoop::main().dispatch([protectedThis = WTFMove(protectedThis), error = WTFMove(error), completionHandler = WTFMove(completionHandler)] () mutable {
     517        auto result = compiledToFile(WTFMove(json), WTFMove(parsedRules), path);
     518        if (!result.has_value()) {
     519            RunLoop::main().dispatch([protectedThis = WTFMove(protectedThis), error = WTFMove(result.error()), completionHandler = WTFMove(completionHandler)] () mutable {
    462520                completionHandler(nullptr, error);
    463521            });
     
    465523        }
    466524
    467         RunLoop::main().dispatch([protectedThis = WTFMove(protectedThis), identifier = WTFMove(identifier), fileData = WTFMove(fileData), metaData = WTFMove(metaData), completionHandler = WTFMove(completionHandler)] () mutable {
    468             RefPtr<API::ContentRuleList> contentRuleList = createExtension(identifier, metaData, fileData);
    469             completionHandler(contentRuleList, { });
     525        RunLoop::main().dispatch([protectedThis = WTFMove(protectedThis), identifier = WTFMove(identifier), data = WTFMove(result.value()), completionHandler = WTFMove(completionHandler)] () mutable {
     526            auto contentRuleList = createExtension(identifier, WTFMove(data));
     527            completionHandler(contentRuleList.ptr(), { });
    470528        });
    471529    });
     
    517575            });
    518576        };
    519        
    520         ContentRuleListMetaData metaData;
    521         WebKit::NetworkCache::Data fileData;
    522         if (!openAndMapContentRuleList(path, metaData, fileData)) {
     577
     578        auto contentRuleList = openAndMapOrCopyContentRuleList(path);
     579        if (!contentRuleList) {
    523580            complete({ });
    524581            return;
    525582        }
    526583       
    527         switch (metaData.version) {
     584        switch (contentRuleList->metaData.version) {
    528585        case 9:
    529586        case 10:
    530             if (!metaData.sourceSize) {
     587            if (!contentRuleList->metaData.sourceSize) {
    531588                complete({ });
    532589                return;
    533590            }
    534             bool is8Bit = fileData.data()[ContentRuleListFileHeaderSize];
     591            bool is8Bit = contentRuleList->dataPointer()[ContentRuleListFileHeaderSize];
    535592            size_t start = ContentRuleListFileHeaderSize + sizeof(bool);
    536             size_t length = metaData.sourceSize - sizeof(bool);
     593            size_t length = contentRuleList->metaData.sourceSize - sizeof(bool);
    537594            if (is8Bit)
    538                 complete(WTF::String(fileData.data() + start, length));
     595                complete(WTF::String(contentRuleList->dataPointer() + start, length));
    539596            else {
    540597                ASSERT(!(length % sizeof(UChar)));
    541                 complete(WTF::String(reinterpret_cast<const UChar*>(fileData.data() + start), length / sizeof(UChar)));
     598                complete(WTF::String(reinterpret_cast<const UChar*>(contentRuleList->dataPointer() + start), length / sizeof(UChar)));
    542599            }
    543600            return;
  • branches/safari-607-branch/Source/WebKit/UIProcess/API/APIContentRuleListStore.h

    r234873 r242860  
    3131#include <system_error>
    3232#include <wtf/text/WTFString.h>
     33
     34namespace WebCore {
     35class SharedBuffer;
     36}
    3337
    3438namespace WTF {
     
    7377    void getContentRuleListSource(const WTF::String& identifier, CompletionHandler<void(WTF::String)>);
    7478
     79    static RefPtr<WebCore::SharedBuffer> readContentsOfFile(const WTF::String& path);
     80
    7581private:
    7682    WTF::String defaultStorePath(bool legacyFilename);
  • branches/safari-607-branch/Source/WebKit/UIProcess/API/Cocoa/APIContentRuleListStoreCocoa.mm

    r235828 r242860  
    3030
    3131#include "SandboxUtilities.h"
     32#include <WebCore/SharedBuffer.h>
    3233
    3334namespace API {
     
    6465}
    6566
     67RefPtr<WebCore::SharedBuffer> ContentRuleListStore::readContentsOfFile(const String& filePath)
     68{
     69    ASSERT(!isMainThread());
     70    NSData *data = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:filePath isDirectory:NO]];
     71    if (!data)
     72        return nullptr;
     73    return WebCore::SharedBuffer::create(data);
     74}
     75
    6676} // namespace API
    6777
  • branches/safari-607-branch/Source/WebKit/UIProcess/API/Cocoa/WKContentRuleListStore.mm

    r235365 r242860  
    3131
    3232#import "APIContentRuleListStore.h"
     33#import "NetworkCacheFilesystem.h"
    3334#import "WKErrorInternal.h"
    3435#import <wtf/BlockPtr.h>
     
    128129// For testing only.
    129130
     131+ (void)_registerPathAsUnsafeToMemoryMapForTesting:(NSString *)filename
     132{
     133    WebKit::NetworkCache::registerPathAsUnsafeToMemoryMapForTesting(filename);
     134}
     135
    130136- (void)_removeAllContentRuleLists
    131137{
  • branches/safari-607-branch/Source/WebKit/UIProcess/API/Cocoa/WKContentRuleListStorePrivate.h

    r216809 r242860  
    3434- (void)_invalidateContentRuleListVersionForIdentifier:(NSString *)identifier;
    3535- (void)_getContentRuleListSourceForIdentifier:(NSString *)identifier completionHandler:(void (^)(NSString*))completionHandler;
     36+ (void)_registerPathAsUnsafeToMemoryMapForTesting:(NSString *)filename;
    3637
    3738// NS_RELEASES_ARGUMENT to keep peak memory usage low.
  • branches/safari-607-branch/Source/WebKit/UIProcess/API/Cocoa/_WKUserContentFilter.mm

    r232520 r242860  
    5959}
    6060
     61- (BOOL)usesCopiedMemory
     62{
     63    return _contentRuleList->_contentRuleList->usesCopiedMemory();
     64}
     65
    6166@end
    6267
  • branches/safari-607-branch/Source/WebKit/UIProcess/API/Cocoa/_WKUserContentFilterPrivate.h

    r221930 r242860  
    3333
    3434- (id)_initWithWKContentRuleList:(WKContentRuleList*)contentRuleList WK_API_AVAILABLE(macosx(10.13), ios(11.0));
     35@property (nonatomic, readonly) BOOL usesCopiedMemory;
    3536
    3637@end
  • branches/safari-607-branch/Source/WebKit/WebProcess/UserContent/WebUserContentController.cpp

    r240768 r242860  
    349349
    350350#if ENABLE(CONTENT_EXTENSIONS)
    351 void WebUserContentController::addContentRuleLists(const Vector<std::pair<String, WebCompiledContentRuleListData>>& contentRuleLists)
    352 {
    353     for (const auto& contentRuleList : contentRuleLists) {
    354         WebCompiledContentRuleListData contentRuleListData = contentRuleList.second;
    355         auto compiledContentRuleList = WebCompiledContentRuleList::create(WTFMove(contentRuleListData));
     351void WebUserContentController::addContentRuleLists(Vector<std::pair<String, WebCompiledContentRuleListData>>&& contentRuleLists)
     352{
     353    for (auto&& contentRuleList : contentRuleLists) {
     354        auto compiledContentRuleList = WebCompiledContentRuleList::create(WTFMove(contentRuleList.second));
    356355
    357356        m_contentExtensionBackend.addContentExtension(contentRuleList.first, WTFMove(compiledContentRuleList));
  • branches/safari-607-branch/Source/WebKit/WebProcess/UserContent/WebUserContentController.h

    r240425 r242860  
    7070    void addUserScriptMessageHandlers(const Vector<WebScriptMessageHandlerData>&);
    7171#if ENABLE(CONTENT_EXTENSIONS)
    72     void addContentRuleLists(const Vector<std::pair<String, WebCompiledContentRuleListData>>&);
     72    void addContentRuleLists(Vector<std::pair<String, WebCompiledContentRuleListData>>&&);
    7373#endif
    7474
  • branches/safari-607-branch/Source/WebKit/WebProcess/WebPage/WebPage.cpp

    r242858 r242860  
    625625    m_userContentController->addUserScriptMessageHandlers(parameters.messageHandlers);
    626626#if ENABLE(CONTENT_EXTENSIONS)
    627     m_userContentController->addContentRuleLists(parameters.contentRuleLists);
     627    m_userContentController->addContentRuleLists(WTFMove(parameters.contentRuleLists));
    628628#endif
    629629
  • branches/safari-607-branch/Tools/ChangeLog

    r242388 r242860  
     12019-03-13  Babak Shafiei  <bshafiei@apple.com>
     2
     3        Cherry-pick r242735. rdar://problem/48780112
     4
     5    Add a WKContentRuleList variant that uses copied memory instead of mmap'd shared memory for class A containerized apps
     6    https://bugs.webkit.org/show_bug.cgi?id=195511
     7    <rdar://problem/44873269>
     8   
     9    Patch by Alex Christensen <achristensen@webkit.org> on 2019-03-11
     10    Reviewed by Darin Adler.
     11   
     12    Source/WebKit:
     13   
     14    * NetworkProcess/NetworkContentRuleListManager.cpp:
     15    (WebKit::NetworkContentRuleListManager::addContentRuleLists):
     16    * NetworkProcess/NetworkContentRuleListManager.h:
     17    * NetworkProcess/cache/NetworkCacheFileSystem.cpp:
     18    (WebKit::NetworkCache::pathRegisteredAsUnsafeToMemoryMapForTesting):
     19    (WebKit::NetworkCache::registerPathAsUnsafeToMemoryMapForTesting):
     20    (WebKit::NetworkCache::isSafeToUseMemoryMapForPath):
     21    * NetworkProcess/cache/NetworkCacheFileSystem.h:
     22    * Shared/WebCompiledContentRuleList.cpp:
     23    (WebKit::WebCompiledContentRuleList::usesCopiedMemory const):
     24    (WebKit::WebCompiledContentRuleList::conditionsApplyOnlyToDomain const):
     25    (WebKit::WebCompiledContentRuleList::filtersWithoutConditionsBytecode const):
     26    (WebKit::WebCompiledContentRuleList::filtersWithConditionsBytecode const):
     27    (WebKit::WebCompiledContentRuleList::topURLFiltersBytecode const):
     28    (WebKit::WebCompiledContentRuleList::actions const):
     29    * Shared/WebCompiledContentRuleList.h:
     30    * Shared/WebCompiledContentRuleListData.cpp:
     31    (WebKit::WebCompiledContentRuleListData::size const):
     32    (WebKit::WebCompiledContentRuleListData::dataPointer const):
     33    (WebKit::WebCompiledContentRuleListData::encode const):
     34    (WebKit::WebCompiledContentRuleListData::decode):
     35    * Shared/WebCompiledContentRuleListData.h:
     36    (WebKit::WebCompiledContentRuleListData::WebCompiledContentRuleListData):
     37    * UIProcess/API/APIContentRuleList.cpp:
     38    (API::ContentRuleList::usesCopiedMemory const):
     39    * UIProcess/API/APIContentRuleList.h:
     40    * UIProcess/API/APIContentRuleListStore.cpp:
     41    (API::getData):
     42    (API::decodeContentRuleListMetaData):
     43    (API::ContentRuleListStore::readContentsOfFile):
     44    (API::MappedOrCopiedData::dataPointer const):
     45    (API::openAndMapOrCopyContentRuleList):
     46    (API::compiledToFile):
     47    (API::createExtension):
     48    (API::ContentRuleListStore::lookupContentRuleList):
     49    (API::ContentRuleListStore::compileContentRuleList):
     50    (API::ContentRuleListStore::getContentRuleListSource):
     51    (API::openAndMapContentRuleList): Deleted.
     52    * UIProcess/API/APIContentRuleListStore.h:
     53    * UIProcess/API/Cocoa/APIContentRuleListStoreCocoa.mm:
     54    (API::ContentRuleListStore::readContentsOfFile):
     55    * UIProcess/API/Cocoa/WKContentRuleListStore.mm:
     56    (+[WKContentRuleListStore _registerPathAsUnsafeToMemoryMapForTesting:]):
     57    * UIProcess/API/Cocoa/WKContentRuleListStorePrivate.h:
     58    * UIProcess/API/Cocoa/_WKUserContentFilter.mm:
     59    (-[_WKUserContentFilter usesCopiedMemory]):
     60    * UIProcess/API/Cocoa/_WKUserContentFilterPrivate.h:
     61    * WebProcess/UserContent/WebUserContentController.cpp:
     62    (WebKit::WebUserContentController::addContentRuleLists):
     63    * WebProcess/UserContent/WebUserContentController.h:
     64    * WebProcess/WebPage/WebPage.cpp:
     65    (WebKit::m_hostFileDescriptor):
     66   
     67    Tools:
     68   
     69    * TestWebKitAPI/Tests/WebKitCocoa/WKContentExtensionStore.mm:
     70    (-[TestSchemeHandlerSubresourceShouldBeBlocked webView:startURLSchemeTask:]):
     71    (-[TestSchemeHandlerSubresourceShouldBeBlocked webView:stopURLSchemeTask:]):
     72    (TEST_F):
     73   
     74    git-svn-id: https://svn.webkit.org/repository/webkit/trunk@242735 268f45cc-cd09-0410-ab3c-d52691b4dbfc
     75
     76    2019-03-11  Alex Christensen  <achristensen@webkit.org>
     77
     78            Add a WKContentRuleList variant that uses copied memory instead of mmap'd shared memory for class A containerized apps
     79            https://bugs.webkit.org/show_bug.cgi?id=195511
     80            <rdar://problem/44873269>
     81
     82            Reviewed by Darin Adler.
     83
     84            * TestWebKitAPI/Tests/WebKitCocoa/WKContentExtensionStore.mm:
     85            (-[TestSchemeHandlerSubresourceShouldBeBlocked webView:startURLSchemeTask:]):
     86            (-[TestSchemeHandlerSubresourceShouldBeBlocked webView:stopURLSchemeTask:]):
     87            (TEST_F):
     88
    1892019-03-04  Kocsen Chung  <kocsen_chung@apple.com>
    290
  • branches/safari-607-branch/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKContentExtensionStore.mm

    r216809 r242860  
    3131#import "PlatformUtilities.h"
    3232#import "Test.h"
     33#import "TestWKWebView.h"
    3334#import <WebKit/WKContentRuleList.h>
    3435#import <WebKit/WKContentRuleListStorePrivate.h>
     36#import <WebKit/_WKUserContentFilterPrivate.h>
    3537#import <wtf/RetainPtr.h>
     38#import <wtf/text/StringBuilder.h>
     39#import <wtf/text/StringConcatenate.h>
     40#import <wtf/text/StringConcatenateNumbers.h>
    3641
    3742class WKContentRuleListStoreTest : public testing::Test {
     
    381386}
    382387
     388@interface TestSchemeHandlerSubresourceShouldBeBlocked : NSObject <WKURLSchemeHandler>
     389@end
     390@implementation TestSchemeHandlerSubresourceShouldBeBlocked
     391- (void)webView:(WKWebView *)webView startURLSchemeTask:(id <WKURLSchemeTask>)task
     392{
     393    EXPECT_TRUE([task.request.URL.path isEqualToString:@"/shouldload"]);
     394    [task didReceiveResponse:[[[NSURLResponse alloc] initWithURL:task.request.URL MIMEType:@"text/html" expectedContentLength:0 textEncodingName:nil] autorelease]];
     395    [task didFinish];
     396}
     397- (void)webView:(WKWebView *)webView stopURLSchemeTask:(id <WKURLSchemeTask>)task
     398{
     399    EXPECT_TRUE(false);
     400}
     401@end
     402
     403TEST_F(WKContentRuleListStoreTest, UnsafeMMap)
     404{
     405    RetainPtr<NSString> tempDir = [NSTemporaryDirectory() stringByAppendingPathComponent:@"UnsafeMMapTest"];
     406    RetainPtr<WKContentRuleListStore> store = [WKContentRuleListStore storeWithURL:[NSURL fileURLWithPath:tempDir.get() isDirectory:YES]];
     407    static NSString *identifier = @"TestRuleList";
     408    static NSString *fileName = @"ContentRuleList-TestRuleList";
     409    static NSString *ruleListSourceString = @"[{\"action\":{\"type\":\"block\"},\"trigger\":{\"url-filter\":\"blockedsubresource\"}}]";
     410    RetainPtr<NSString> filePath = [tempDir stringByAppendingPathComponent:fileName];
     411
     412    auto runTest = [&] (bool shouldUseCopiedMemory) {
     413        EXPECT_FALSE([[NSFileManager defaultManager] fileExistsAtPath:filePath.get()]);
     414       
     415        __block bool doneCompiling = false;
     416        __block RetainPtr<WKContentRuleList> ruleList;
     417        [store compileContentRuleListForIdentifier:identifier encodedContentRuleList:ruleListSourceString completionHandler:^(WKContentRuleList *filter, NSError *error) {
     418            EXPECT_NOT_NULL(filter);
     419            EXPECT_NULL(error);
     420            doneCompiling = true;
     421            ruleList = filter;
     422            EXPECT_TRUE([[[[_WKUserContentFilter alloc] _initWithWKContentRuleList:filter] autorelease] usesCopiedMemory] == shouldUseCopiedMemory);
     423        }];
     424        TestWebKitAPI::Util::run(&doneCompiling);
     425       
     426        EXPECT_TRUE([[NSFileManager defaultManager] fileExistsAtPath:filePath.get()]);
     427
     428        auto handler = adoptNS([TestSchemeHandlerSubresourceShouldBeBlocked new]);
     429        auto configuration = adoptNS([WKWebViewConfiguration new]);
     430        [configuration setURLSchemeHandler:handler.get() forURLScheme:@"testmmap"];
     431        [[configuration userContentController] addContentRuleList:ruleList.get()];
     432        auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
     433        [webView synchronouslyLoadHTMLString:@"<html>main resource content</html>" baseURL:[NSURL URLWithString:@"testmmap://webkit.org/mainresource"]];
     434
     435        auto loadingShouldSucceed = [&] (NSString *resourcePath, NSString *shouldSucceed) {
     436            __block bool doneEvaluating = false;
     437            [webView evaluateJavaScript:[NSString stringWithFormat:@"var caught = false; var xhr = new XMLHttpRequest(); xhr.open('GET', '%@', false); try{ xhr.send() } catch(e) { caught = true; }; caught != %@ ? 'success' : 'failure'", resourcePath, shouldSucceed] completionHandler:^(id result, NSError *error) {
     438                EXPECT_NULL(error);
     439                EXPECT_TRUE([@"success" isEqualToString:result]);
     440                doneEvaluating = true;
     441            }];
     442            TestWebKitAPI::Util::run(&doneEvaluating);
     443        };
     444        loadingShouldSucceed(@"/shouldload", @"true");
     445        loadingShouldSucceed(@"/blockedsubresource", @"false");
     446
     447        [[configuration userContentController] removeContentRuleList:ruleList.get()];
     448       
     449        __block bool doneLookingUp = false;
     450        [store lookUpContentRuleListForIdentifier:identifier completionHandler:^(WKContentRuleList *filter, NSError *error) {
     451            EXPECT_NOT_NULL(filter);
     452            EXPECT_NULL(error);
     453           
     454            doneLookingUp = true;
     455           
     456            EXPECT_TRUE([[[[_WKUserContentFilter alloc] _initWithWKContentRuleList:filter] autorelease] usesCopiedMemory] == shouldUseCopiedMemory);
     457            ruleList = filter;
     458        }];
     459        TestWebKitAPI::Util::run(&doneLookingUp);
     460
     461        [[configuration userContentController] addContentRuleList:ruleList.get()];
     462        loadingShouldSucceed(@"/shouldload", @"true");
     463        loadingShouldSucceed(@"/blockedsubresource", @"false");
     464
     465        __block bool doneCheckingSource = false;
     466        [store _getContentRuleListSourceForIdentifier:identifier completionHandler:^(NSString *source) {
     467            EXPECT_TRUE([source isEqualToString:ruleListSourceString]);
     468            doneCheckingSource = true;
     469        }];
     470        TestWebKitAPI::Util::run(&doneCheckingSource);
     471       
     472        __block bool doneRemoving = false;
     473        [store removeContentRuleListForIdentifier:identifier completionHandler:^(NSError *error) {
     474            EXPECT_NULL(error);
     475            doneRemoving = true;
     476        }];
     477        TestWebKitAPI::Util::run(&doneRemoving);
     478
     479        EXPECT_FALSE([[NSFileManager defaultManager] fileExistsAtPath:filePath.get()]);
     480    };
     481   
     482    runTest(false);
     483    [WKContentRuleListStore _registerPathAsUnsafeToMemoryMapForTesting:filePath.get()];
     484    runTest(true);
     485}
    383486
    384487#endif
Note: See TracChangeset for help on using the changeset viewer.