- Revision
- 179702
- Author
- [email protected]
- Date
- 2015-02-05 13:29:19 -0800 (Thu, 05 Feb 2015)
Log Message
Free memory read under MemoryCache::pruneLiveResourcesToSize()
https://bugs.webkit.org/show_bug.cgi?id=141292
<rdar://problem/19725522>
Reviewed by Antti Koivisto.
In MemoryCache::pruneLiveResourcesToSize(), we were iterating over the
m_liveDecodedResources ListHashSet and possibly calling
CachedResource::destroyDecodedData() on the current value. Doing so
would cause a call to ListHashSet::remove() to remove the value pointed
by the current iterator, thus invalidating our iterator.
In this patch, we increment the ListHashSet iterator *before* calling
CachedResource::destroyDecodedData(), while the current iterator is
still valid. Note that this is safe because unlike iteration of most
WTF Hash data structures, iteration is guaranteed safe against mutation
of the ListHashSet, except for removal of the item currently pointed to
by a given iterator.
Test: http/tests/cache/memory-cache-pruning.html
* loader/cache/MemoryCache.cpp:
(WebCore::MemoryCache::pruneLiveResourcesToSize):
Modified Paths
Added Paths
Diff
Added: trunk/LayoutTests/http/tests/cache/memory-cache-pruning-expected.txt (0 => 179702)
--- trunk/LayoutTests/http/tests/cache/memory-cache-pruning-expected.txt (rev 0)
+++ trunk/LayoutTests/http/tests/cache/memory-cache-pruning-expected.txt 2015-02-05 21:29:19 UTC (rev 179702)
@@ -0,0 +1,11 @@
+This test exercices the code path executed when the memory cache reaches its maximum size
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+PASS previousSize > 0 is true
+PASS internals.memoryCacheSize() < previousSize is true
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
Added: trunk/LayoutTests/http/tests/cache/memory-cache-pruning.html (0 => 179702)
--- trunk/LayoutTests/http/tests/cache/memory-cache-pruning.html (rev 0)
+++ trunk/LayoutTests/http/tests/cache/memory-cache-pruning.html 2015-02-05 21:29:19 UTC (rev 179702)
@@ -0,0 +1,27 @@
+<!DOCTYPE html>
+<html>
+<head>
+<link rel=stylesheet href=""
+<script src=""
+<script>
+function runTest() {
+ previousSize = internals.memoryCacheSize();
+ shouldBeTrue("previousSize > 0");
+ internals.pruneMemoryCacheToSize(0);
+ shouldBeTrue("internals.memoryCacheSize() < previousSize");
+ finishJSTest();
+}
+</script>
+</head>
+<body _onload_="runTest()">
+<img src=""
+<img src=""
+<audio src=""
+<video src=""
+<script>
+description("This test exercices the code path executed when the memory cache reaches its maximum size");
+jsTestIsAsync = true;
+</script>
+<script src=""
+</body>
+</html>
Modified: trunk/Source/WebCore/ChangeLog (179701 => 179702)
--- trunk/Source/WebCore/ChangeLog 2015-02-05 20:12:38 UTC (rev 179701)
+++ trunk/Source/WebCore/ChangeLog 2015-02-05 21:29:19 UTC (rev 179702)
@@ -1,3 +1,29 @@
+2015-02-05 Chris Dumez <[email protected]>
+
+ Free memory read under MemoryCache::pruneLiveResourcesToSize()
+ https://bugs.webkit.org/show_bug.cgi?id=141292
+ <rdar://problem/19725522>
+
+ Reviewed by Antti Koivisto.
+
+ In MemoryCache::pruneLiveResourcesToSize(), we were iterating over the
+ m_liveDecodedResources ListHashSet and possibly calling
+ CachedResource::destroyDecodedData() on the current value. Doing so
+ would cause a call to ListHashSet::remove() to remove the value pointed
+ by the current iterator, thus invalidating our iterator.
+
+ In this patch, we increment the ListHashSet iterator *before* calling
+ CachedResource::destroyDecodedData(), while the current iterator is
+ still valid. Note that this is safe because unlike iteration of most
+ WTF Hash data structures, iteration is guaranteed safe against mutation
+ of the ListHashSet, except for removal of the item currently pointed to
+ by a given iterator.
+
+ Test: http/tests/cache/memory-cache-pruning.html
+
+ * loader/cache/MemoryCache.cpp:
+ (WebCore::MemoryCache::pruneLiveResourcesToSize):
+
2015-02-05 Jer Noble <[email protected]>
[Mac] HLS <video> will not fire 'progress' events, only 'stalled'.
Modified: trunk/Source/WebCore/WebCore.exp.in (179701 => 179702)
--- trunk/Source/WebCore/WebCore.exp.in 2015-02-05 20:12:38 UTC (rev 179701)
+++ trunk/Source/WebCore/WebCore.exp.in 2015-02-05 21:29:19 UTC (rev 179702)
@@ -197,6 +197,8 @@
__ZN7WebCore11MemoryCache18resourceForRequestERKNS_15ResourceRequestENS_9SessionIDE
__ZN7WebCore11MemoryCache19getOriginsWithCacheERN3WTF7HashSetINS1_6RefPtrINS_14SecurityOriginEEENS_18SecurityOriginHashENS1_10HashTraitsIS5_EEEE
__ZN7WebCore11MemoryCache20removeImageFromCacheERKNS_3URLERKN3WTF6StringE
+__ZN7WebCore11MemoryCache24pruneDeadResourcesToSizeEj
+__ZN7WebCore11MemoryCache24pruneLiveResourcesToSizeEjb
__ZN7WebCore11MemoryCache25removeResourcesWithOriginERNS_14SecurityOriginE
__ZN7WebCore11MemoryCache9singletonEv
__ZN7WebCore11PageOverlay15setNeedsDisplayERKNS_7IntRectE
Modified: trunk/Source/WebCore/loader/cache/MemoryCache.cpp (179701 => 179702)
--- trunk/Source/WebCore/loader/cache/MemoryCache.cpp 2015-02-05 20:12:38 UTC (rev 179701)
+++ trunk/Source/WebCore/loader/cache/MemoryCache.cpp 2015-02-05 21:29:19 UTC (rev 179702)
@@ -300,7 +300,18 @@
// elapsedTime will evaluate to false as the currentTime will be a lot
// greater than the current->m_lastDecodedAccessTime.
// For more details see: https://bugs.webkit.org/show_bug.cgi?id=30209
- for (auto* current : m_liveDecodedResources) {
+ auto it = m_liveDecodedResources.begin();
+ while (it != m_liveDecodedResources.end()) {
+ auto* current = *it;
+
+ // Increment the iterator now because the call to destroyDecodedData() below
+ // may cause a call to ListHashSet::remove() and invalidate the current
+ // iterator. Note that this is safe because unlike iteration of most
+ // WTF Hash data structures, iteration is guaranteed safe against mutation
+ // of the ListHashSet, except for removal of the item currently pointed to
+ // by a given iterator.
+ ++it;
+
ASSERT(current->hasClients());
if (current->isLoaded() && current->decodedSize()) {
// Check to see if the remaining resources are too new to prune.
@@ -311,9 +322,8 @@
if (current->decodedDataIsPurgeable())
continue;
- // Destroy our decoded data. This will remove us from
- // m_liveDecodedResources, and possibly move us to a different LRU
- // list in m_allResources.
+ // Destroy our decoded data. This will remove us from m_liveDecodedResources, and possibly move us
+ // to a different LRU list in m_allResources.
current->destroyDecodedData();
if (targetSize && m_liveSize <= targetSize)
Modified: trunk/Source/WebCore/loader/cache/MemoryCache.h (179701 => 179702)
--- trunk/Source/WebCore/loader/cache/MemoryCache.h 2015-02-05 20:12:38 UTC (rev 179701)
+++ trunk/Source/WebCore/loader/cache/MemoryCache.h 2015-02-05 21:29:19 UTC (rev 179702)
@@ -62,7 +62,7 @@
class MemoryCache {
WTF_MAKE_NONCOPYABLE(MemoryCache); WTF_MAKE_FAST_ALLOCATED;
friend NeverDestroyed<MemoryCache>;
-
+ friend class Internals;
public:
struct TypeStatistic {
int count;
@@ -117,6 +117,7 @@
WEBCORE_EXPORT void evictResources();
void prune();
+ unsigned size() const { return m_liveSize + m_deadSize; }
void setDeadDecodedDataDeletionInterval(std::chrono::milliseconds interval) { m_deadDecodedDataDeletionInterval = interval; }
std::chrono::milliseconds deadDecodedDataDeletionInterval() const { return m_deadDecodedDataDeletionInterval; }
Modified: trunk/Source/WebCore/testing/Internals.cpp (179701 => 179702)
--- trunk/Source/WebCore/testing/Internals.cpp 2015-02-05 20:12:38 UTC (rev 179701)
+++ trunk/Source/WebCore/testing/Internals.cpp 2015-02-05 21:29:19 UTC (rev 179702)
@@ -409,6 +409,17 @@
MemoryCache::singleton().evictResources();
}
+void Internals::pruneMemoryCacheToSize(unsigned size)
+{
+ MemoryCache::singleton().pruneDeadResourcesToSize(size);
+ MemoryCache::singleton().pruneLiveResourcesToSize(size, true);
+}
+
+unsigned Internals::memoryCacheSize() const
+{
+ return MemoryCache::singleton().size();
+}
+
Node* Internals::treeScopeRootNode(Node* node, ExceptionCode& ec)
{
if (!node) {
Modified: trunk/Source/WebCore/testing/Internals.h (179701 => 179702)
--- trunk/Source/WebCore/testing/Internals.h 2015-02-05 20:12:38 UTC (rev 179701)
+++ trunk/Source/WebCore/testing/Internals.h 2015-02-05 21:29:19 UTC (rev 179702)
@@ -84,7 +84,10 @@
bool isPreloaded(const String& url);
bool isLoadingFromMemoryCache(const String& url);
String xhrResponseSource(XMLHttpRequest*);
+
void clearMemoryCache();
+ void pruneMemoryCacheToSize(unsigned size);
+ unsigned memoryCacheSize() const;
PassRefPtr<CSSComputedStyleDeclaration> computedStyleIncludingVisitedInfo(Node*, ExceptionCode&) const;
Modified: trunk/Source/WebCore/testing/Internals.idl (179701 => 179702)
--- trunk/Source/WebCore/testing/Internals.idl 2015-02-05 20:12:38 UTC (rev 179701)
+++ trunk/Source/WebCore/testing/Internals.idl 2015-02-05 21:29:19 UTC (rev 179702)
@@ -44,6 +44,8 @@
boolean isLoadingFromMemoryCache(DOMString url);
DOMString xhrResponseSource(XMLHttpRequest xhr);
void clearMemoryCache();
+ void pruneMemoryCacheToSize(long size);
+ long memoryCacheSize();
[RaisesException] CSSStyleDeclaration computedStyleIncludingVisitedInfo(Node node);