<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[200749] branches/safari-601.1.46-branch/Source/WebCore</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/200749">200749</a></dd>
<dt>Author</dt> <dd>matthew_hanson@apple.com</dd>
<dt>Date</dt> <dd>2016-05-12 02:11:54 -0700 (Thu, 12 May 2016)</dd>
</dl>

<h3>Log Message</h3>
<pre>Merge <a href="http://trac.webkit.org/projects/webkit/changeset/194290">r194290</a>. rdar://problem/26228866</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#branchessafari601146branchSourceWebCoreChangeLog">branches/safari-601.1.46-branch/Source/WebCore/ChangeLog</a></li>
<li><a href="#branchessafari601146branchSourceWebCorehtmlHTMLCanvasElementcpp">branches/safari-601.1.46-branch/Source/WebCore/html/HTMLCanvasElement.cpp</a></li>
<li><a href="#branchessafari601146branchSourceWebCorehtmlHTMLCanvasElementh">branches/safari-601.1.46-branch/Source/WebCore/html/HTMLCanvasElement.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="branchessafari601146branchSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: branches/safari-601.1.46-branch/Source/WebCore/ChangeLog (200748 => 200749)</h4>
<pre class="diff"><span>
<span class="info">--- branches/safari-601.1.46-branch/Source/WebCore/ChangeLog        2016-05-12 09:11:50 UTC (rev 200748)
+++ branches/safari-601.1.46-branch/Source/WebCore/ChangeLog        2016-05-12 09:11:54 UTC (rev 200749)
</span><span class="lines">@@ -1,5 +1,34 @@
</span><span class="cx"> 2016-05-12  Matthew Hanson  &lt;matthew_hanson@apple.com&gt;
</span><span class="cx"> 
</span><ins>+        Merge r194290. rdar://problem/26228866
+
+    2015-12-18  Brent Fulgham  &lt;bfulgham@apple.com&gt;
+
+            Place an upper bound on canvas pixel count
+            https://bugs.webkit.org/show_bug.cgi?id=151825
+            &lt;rdar://problem/23324916&gt;
+
+            Reviewed by Simon Fraser (Relanded by Brent Fulgham)
+
+            Malformed JavaScript can attempt to create lots of canvas contexts. Limit the amount of memory
+            we will use for this purpose to some percentage of system RAM.
+
+            * html/HTMLCanvasElement.cpp:
+            (WebCore::removeFromActivePixelMemory): Added helper function
+            (WebCore::HTMLCanvasElement::~HTMLCanvasElement): Call new 'releaseImageBufferAndContext' method
+            to ensure ImageBuffer and graphics context state are properly cleaned up.
+            (WebCore::maxActivePixels): Use one quarter of the system RAM, or 2 GB (whichever is more) as
+            an upper bound on active pixel memory.
+            (WebCore::HTMLCanvasElement::getContext): If we are attempting to create a context that will cause
+            us to exceed the allowed active pixel count, fail.
+            (WebCore::HTMLCanvasElement::releaseImageBufferAndContext): Added helper function
+            (WebCore::HTMLCanvasElement::setSurfaceSize): Use the new 'releaseImageBufferAndContext' method
+            to handle active pixel memory counts.
+            (WebCore::HTMLCanvasElement::createImageBuffer): Refuse to create a backing buffer if it will
+            exceed our available pixel memory.
+
+2016-05-12  Matthew Hanson  &lt;matthew_hanson@apple.com&gt;
+
</ins><span class="cx">         Merge r190820. rdar://problem/26228881
</span><span class="cx"> 
</span><span class="cx">     2015-10-09  Simon Fraser  &lt;simon.fraser@apple.com&gt;
</span></span></pre></div>
<a id="branchessafari601146branchSourceWebCorehtmlHTMLCanvasElementcpp"></a>
<div class="modfile"><h4>Modified: branches/safari-601.1.46-branch/Source/WebCore/html/HTMLCanvasElement.cpp (200748 => 200749)</h4>
<pre class="diff"><span>
<span class="info">--- branches/safari-601.1.46-branch/Source/WebCore/html/HTMLCanvasElement.cpp        2016-05-12 09:11:50 UTC (rev 200748)
+++ branches/safari-601.1.46-branch/Source/WebCore/html/HTMLCanvasElement.cpp        2016-05-12 09:11:54 UTC (rev 200749)
</span><span class="lines">@@ -47,6 +47,7 @@
</span><span class="cx"> #include &quot;ScriptController.h&quot;
</span><span class="cx"> #include &quot;Settings.h&quot;
</span><span class="cx"> #include &lt;math.h&gt;
</span><ins>+#include &lt;wtf/RAMSize.h&gt;
</ins><span class="cx"> 
</span><span class="cx"> #include &lt;runtime/JSCInlines.h&gt;
</span><span class="cx"> #include &lt;runtime/JSLock.h&gt;
</span><span class="lines">@@ -75,6 +76,8 @@
</span><span class="cx"> static const unsigned MaxCanvasArea = 16384 * 16384;
</span><span class="cx"> #endif
</span><span class="cx"> 
</span><ins>+static size_t activePixelMemory = 0;
+
</ins><span class="cx"> HTMLCanvasElement::HTMLCanvasElement(const QualifiedName&amp; tagName, Document&amp; document)
</span><span class="cx">     : HTMLElement(tagName, document)
</span><span class="cx">     , m_size(DefaultWidth, DefaultHeight)
</span><span class="lines">@@ -97,12 +100,25 @@
</span><span class="cx">     return adoptRef(*new HTMLCanvasElement(tagName, document));
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+static void removeFromActivePixelMemory(size_t pixelsReleased)
+{
+    if (!pixelsReleased)
+        return;
+
+    if (pixelsReleased &lt; activePixelMemory)
+        activePixelMemory -= pixelsReleased;
+    else
+        activePixelMemory = 0;
+}
+    
</ins><span class="cx"> HTMLCanvasElement::~HTMLCanvasElement()
</span><span class="cx"> {
</span><span class="cx">     for (auto it = m_observers.begin(), end = m_observers.end(); it != end; ++it)
</span><span class="cx">         (*it)-&gt;canvasDestroyed(*this);
</span><span class="cx"> 
</span><span class="cx">     m_context = nullptr; // Ensure this goes away before the ImageBuffer.
</span><ins>+
+    releaseImageBufferAndContext();
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void HTMLCanvasElement::parseAttribute(const QualifiedName&amp; name, const AtomicString&amp; value)
</span><span class="lines">@@ -179,6 +195,16 @@
</span><span class="cx"> }
</span><span class="cx"> #endif
</span><span class="cx"> 
</span><ins>+static inline size_t maxActivePixelMemory()
+{
+    static size_t maxPixelMemory;
+    static std::once_flag onceFlag;
+    std::call_once(onceFlag, [] {
+        maxPixelMemory = std::max(ramSize() / 4, 2151 * MB);
+    });
+    return maxPixelMemory;
+}
+
</ins><span class="cx"> CanvasRenderingContext* HTMLCanvasElement::getContext(const String&amp; type, CanvasContextAttributes* attrs)
</span><span class="cx"> {
</span><span class="cx">     // A Canvas can either be &quot;2D&quot; or &quot;webgl&quot; but never both. If you request a 2D canvas and the existing
</span><span class="lines">@@ -198,6 +224,18 @@
</span><span class="cx">             if (Settings* settings = document().settings())
</span><span class="cx">                 usesDashbardCompatibilityMode = settings-&gt;usesDashboardBackwardCompatibilityMode();
</span><span class="cx"> #endif
</span><ins>+
+            // Make sure we don't use more pixel memory than the system can support.
+            size_t requestedPixelMemory = 4 * width() * height();
+            if (activePixelMemory + requestedPixelMemory &gt; maxActivePixelMemory()) {
+                StringBuilder stringBuilder;
+                stringBuilder.appendLiteral(&quot;Total canvas memory use exceeds the maximum limit (&quot;);
+                stringBuilder.appendNumber(maxActivePixelMemory() / 1024 / 1024);
+                stringBuilder.appendLiteral(&quot; MB).&quot;);
+                document().addConsoleMessage(MessageSource::JS, MessageLevel::Warning, stringBuilder.toString());
+                return nullptr;
+            }
+
</ins><span class="cx">             m_context = std::make_unique&lt;CanvasRenderingContext2D&gt;(this, document().inQuirksMode(), usesDashbardCompatibilityMode);
</span><span class="cx"> #if USE(IOSURFACE_CANVAS_BACKING_STORE) || ENABLE(ACCELERATED_2D_CANVAS)
</span><span class="cx">             // Need to make sure a RenderLayer and compositing layer get created for the Canvas
</span><span class="lines">@@ -426,12 +464,17 @@
</span><span class="cx">     m_presentedImage = nullptr;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void HTMLCanvasElement::releaseImageBufferAndContext()
+{
+    m_contextStateSaver = nullptr;
+    setImageBuffer(nullptr);
+}
+    
</ins><span class="cx"> void HTMLCanvasElement::setSurfaceSize(const IntSize&amp; size)
</span><span class="cx"> {
</span><span class="cx">     m_size = size;
</span><span class="cx">     m_hasCreatedImageBuffer = false;
</span><del>-    m_contextStateSaver = nullptr;
-    m_imageBuffer.reset();
</del><ins>+    releaseImageBufferAndContext();
</ins><span class="cx">     clearCopiedImage();
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -571,13 +614,24 @@
</span><span class="cx">         document().addConsoleMessage(MessageSource::JS, MessageLevel::Warning, stringBuilder.toString());
</span><span class="cx">         return;
</span><span class="cx">     }
</span><ins>+    
+    // Make sure we don't use more pixel memory than the system can support.
+    size_t requestedPixelMemory = 4 * width() * height();
+    if (activePixelMemory + requestedPixelMemory &gt; maxActivePixelMemory()) {
+        StringBuilder stringBuilder;
+        stringBuilder.appendLiteral(&quot;Total canvas memory use exceeds the maximum limit (&quot;);
+        stringBuilder.appendNumber(maxActivePixelMemory() / 1024 / 1024);
+        stringBuilder.appendLiteral(&quot; MB).&quot;);
+        document().addConsoleMessage(MessageSource::JS, MessageLevel::Warning, stringBuilder.toString());
+        return;
+    }
</ins><span class="cx"> 
</span><span class="cx">     IntSize bufferSize(deviceSize.width(), deviceSize.height());
</span><span class="cx">     if (!bufferSize.width() || !bufferSize.height())
</span><span class="cx">         return;
</span><span class="cx"> 
</span><span class="cx">     RenderingMode renderingMode = shouldAccelerate(bufferSize) ? Accelerated : Unaccelerated;
</span><del>-    m_imageBuffer = ImageBuffer::create(size(), 1, ColorSpaceDeviceRGB, renderingMode);
</del><ins>+    setImageBuffer(ImageBuffer::create(size(), 1, ColorSpaceDeviceRGB, renderingMode));
</ins><span class="cx">     if (!m_imageBuffer)
</span><span class="cx">         return;
</span><span class="cx">     m_imageBuffer-&gt;context()-&gt;setShadowsIgnoreTransforms(true);
</span><span class="lines">@@ -597,6 +651,15 @@
</span><span class="cx"> #endif
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void HTMLCanvasElement::setImageBuffer(std::unique_ptr&lt;ImageBuffer&gt; buffer) const
+{
+    removeFromActivePixelMemory(memoryCost());
+
+    m_imageBuffer = WTF::move(buffer);
+
+    activePixelMemory += memoryCost();
+}
+
</ins><span class="cx"> GraphicsContext* HTMLCanvasElement::drawingContext() const
</span><span class="cx"> {
</span><span class="cx">     return buffer() ? m_imageBuffer-&gt;context() : nullptr;
</span></span></pre></div>
<a id="branchessafari601146branchSourceWebCorehtmlHTMLCanvasElementh"></a>
<div class="modfile"><h4>Modified: branches/safari-601.1.46-branch/Source/WebCore/html/HTMLCanvasElement.h (200748 => 200749)</h4>
<pre class="diff"><span>
<span class="info">--- branches/safari-601.1.46-branch/Source/WebCore/html/HTMLCanvasElement.h        2016-05-12 09:11:50 UTC (rev 200748)
+++ branches/safari-601.1.46-branch/Source/WebCore/html/HTMLCanvasElement.h        2016-05-12 09:11:54 UTC (rev 200749)
</span><span class="lines">@@ -152,6 +152,8 @@
</span><span class="cx">     void clearImageBuffer() const;
</span><span class="cx"> 
</span><span class="cx">     void setSurfaceSize(const IntSize&amp;);
</span><ins>+    void setImageBuffer(std::unique_ptr&lt;ImageBuffer&gt;) const;
+    void releaseImageBufferAndContext();
</ins><span class="cx"> 
</span><span class="cx">     bool paintsIntoCanvasBuffer() const;
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>