<!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>[194290] trunk/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/194290">194290</a></dd>
<dt>Author</dt> <dd>bfulgham@apple.com</dd>
<dt>Date</dt> <dd>2015-12-18 13:28:54 -0800 (Fri, 18 Dec 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>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.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCorehtmlHTMLCanvasElementcpp">trunk/Source/WebCore/html/HTMLCanvasElement.cpp</a></li>
<li><a href="#trunkSourceWebCorehtmlHTMLCanvasElementh">trunk/Source/WebCore/html/HTMLCanvasElement.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (194289 => 194290)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2015-12-18 21:11:20 UTC (rev 194289)
+++ trunk/Source/WebCore/ChangeLog        2015-12-18 21:28:54 UTC (rev 194290)
</span><span class="lines">@@ -1,3 +1,28 @@
</span><ins>+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.
+
</ins><span class="cx"> 2015-12-18  Olivier Blin  &lt;olivier.blin@softathome.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Fix the !ENABLE(VIDEO) build after r192953 for &lt;picture&gt; element introduction
</span></span></pre></div>
<a id="trunkSourceWebCorehtmlHTMLCanvasElementcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/html/HTMLCanvasElement.cpp (194289 => 194290)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/html/HTMLCanvasElement.cpp        2015-12-18 21:11:20 UTC (rev 194289)
+++ trunk/Source/WebCore/html/HTMLCanvasElement.cpp        2015-12-18 21:28:54 UTC (rev 194290)
</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&amp; observer : m_observers)
</span><span class="cx">         observer-&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,25 @@
</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(), renderingMode);
</del><ins>+
+    setImageBuffer(ImageBuffer::create(size(), renderingMode));
</ins><span class="cx">     if (!m_imageBuffer)
</span><span class="cx">         return;
</span><span class="cx">     m_imageBuffer-&gt;context().setShadowsIgnoreTransforms(true);
</span><span class="lines">@@ -597,6 +652,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() ? &amp;m_imageBuffer-&gt;context() : nullptr;
</span></span></pre></div>
<a id="trunkSourceWebCorehtmlHTMLCanvasElementh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/html/HTMLCanvasElement.h (194289 => 194290)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/html/HTMLCanvasElement.h        2015-12-18 21:11:20 UTC (rev 194289)
+++ trunk/Source/WebCore/html/HTMLCanvasElement.h        2015-12-18 21:28:54 UTC (rev 194290)
</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>