<!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>[213508] 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/213508">213508</a></dd>
<dt>Author</dt> <dd>aestes@apple.com</dd>
<dt>Date</dt> <dd>2017-03-07 01:51:17 -0800 (Tue, 07 Mar 2017)</dd>
</dl>

<h3>Log Message</h3>
<pre>REGRESSION (<a href="http://trac.webkit.org/projects/webkit/changeset/213412">r213412</a>): JPEG premultiplication tests fail under GuardMalloc
https://bugs.webkit.org/show_bug.cgi?id=169266

Reviewed by Tim Horton.

<a href="http://trac.webkit.org/projects/webkit/changeset/213412">r213412</a> added functions to create a CGImageRef from a canvas image buffer, which were used
to create data: URLs and blobs. If JPEG premultiplication is applied, though,
WebCore::cgImage() would return an image with a data provider pointing to stack memory.

I fixed a similar issue in ImageBuffer::toCGImage() by leaking the reference to a
Uint8ClampedArray then creating a CGDataProviderRef with a releaseData function that adopts
the leaked reference.

We could take that approach in cgImage() too by allocating the premultiplied data buffer on
the heap then freeing it with a releaseData function, but it's simpler to refactor
toCGImage()/cgImage() to return an encoded image as a CFDataRef instead of a CFImageRef.
This ensures that a CGDataProviderRef never outlives the data it points to.

Covered by existing tests.

* platform/graphics/ImageBuffer.h: Renamed toCGImage() to toCFData().
* platform/graphics/cg/ImageBufferCG.cpp:
(WebCore::dataURL): Changed to take a CFDataRef.
(WebCore::dataVector): Renamed from data(). Changed to take a CFDataRef.
(WebCore::ImageBuffer::toDataURL): Changed to call toCFData().
(WebCore::ImageBuffer::toData): Ditto.
(WebCore::ImageBuffer::toCFData): Changed to return a CFDataRef after encoding the
CGImageRef that was previously returned.
(WebCore::cfData): Ditto.
(WebCore::dataURL): Changed to call cfData().
(WebCore::data): Ditto.
(WebCore::ImageBuffer::toCGImage): Renamed to ImageBuffer::toCFData().
(WebCore::cgImage): Renamed to WebCore::cfData().</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreplatformgraphicsImageBufferh">trunk/Source/WebCore/platform/graphics/ImageBuffer.h</a></li>
<li><a href="#trunkSourceWebCoreplatformgraphicscgImageBufferCGcpp">trunk/Source/WebCore/platform/graphics/cg/ImageBufferCG.cpp</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (213507 => 213508)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2017-03-07 08:47:03 UTC (rev 213507)
+++ trunk/Source/WebCore/ChangeLog        2017-03-07 09:51:17 UTC (rev 213508)
</span><span class="lines">@@ -1,3 +1,39 @@
</span><ins>+2017-03-07  Andy Estes  &lt;aestes@apple.com&gt;
+
+        REGRESSION (r213412): JPEG premultiplication tests fail under GuardMalloc
+        https://bugs.webkit.org/show_bug.cgi?id=169266
+
+        Reviewed by Tim Horton.
+
+        r213412 added functions to create a CGImageRef from a canvas image buffer, which were used
+        to create data: URLs and blobs. If JPEG premultiplication is applied, though,
+        WebCore::cgImage() would return an image with a data provider pointing to stack memory.
+
+        I fixed a similar issue in ImageBuffer::toCGImage() by leaking the reference to a
+        Uint8ClampedArray then creating a CGDataProviderRef with a releaseData function that adopts
+        the leaked reference.
+
+        We could take that approach in cgImage() too by allocating the premultiplied data buffer on
+        the heap then freeing it with a releaseData function, but it's simpler to refactor
+        toCGImage()/cgImage() to return an encoded image as a CFDataRef instead of a CFImageRef.
+        This ensures that a CGDataProviderRef never outlives the data it points to.
+
+        Covered by existing tests.
+
+        * platform/graphics/ImageBuffer.h: Renamed toCGImage() to toCFData().
+        * platform/graphics/cg/ImageBufferCG.cpp:
+        (WebCore::dataURL): Changed to take a CFDataRef.
+        (WebCore::dataVector): Renamed from data(). Changed to take a CFDataRef.
+        (WebCore::ImageBuffer::toDataURL): Changed to call toCFData().
+        (WebCore::ImageBuffer::toData): Ditto.
+        (WebCore::ImageBuffer::toCFData): Changed to return a CFDataRef after encoding the
+        CGImageRef that was previously returned.
+        (WebCore::cfData): Ditto.
+        (WebCore::dataURL): Changed to call cfData().
+        (WebCore::data): Ditto.
+        (WebCore::ImageBuffer::toCGImage): Renamed to ImageBuffer::toCFData().
+        (WebCore::cgImage): Renamed to WebCore::cfData().
+
</ins><span class="cx"> 2017-03-07  Myles C. Maxfield  &lt;mmaxfield@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Test fix after r213505
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformgraphicsImageBufferh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/graphics/ImageBuffer.h (213507 => 213508)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/graphics/ImageBuffer.h        2017-03-07 08:47:03 UTC (rev 213507)
+++ trunk/Source/WebCore/platform/graphics/ImageBuffer.h        2017-03-07 09:51:17 UTC (rev 213508)
</span><span class="lines">@@ -175,7 +175,7 @@
</span><span class="cx">     WEBCORE_EXPORT ImageBuffer(const FloatSize&amp;, float resolutionScale, ColorSpace, RenderingMode, bool&amp; success);
</span><span class="cx"> #if USE(CG)
</span><span class="cx">     ImageBuffer(const FloatSize&amp;, float resolutionScale, CGColorSpaceRef, RenderingMode, bool&amp; success);
</span><del>-    RetainPtr&lt;CGImageRef&gt; toCGImage(const String&amp; mimeType) const;
</del><ins>+    RetainPtr&lt;CFDataRef&gt; toCFData(const String&amp; mimeType, std::optional&lt;double&gt; quality) const;
</ins><span class="cx"> #elif USE(DIRECT2D)
</span><span class="cx">     ImageBuffer(const FloatSize&amp;, float resolutionScale, ColorSpace, RenderingMode, const GraphicsContext*, bool&amp; success);
</span><span class="cx"> #endif
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformgraphicscgImageBufferCGcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/graphics/cg/ImageBufferCG.cpp (213507 => 213508)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/graphics/cg/ImageBufferCG.cpp        2017-03-07 08:47:03 UTC (rev 213507)
+++ trunk/Source/WebCore/platform/graphics/cg/ImageBufferCG.cpp        2017-03-07 09:51:17 UTC (rev 213508)
</span><span class="lines">@@ -483,50 +483,36 @@
</span><span class="cx">     return CGImageDestinationFinalize(destination.get());
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-static String dataURL(CGImageRef image, const String&amp; mimeType, std::optional&lt;double&gt; quality)
</del><ins>+static String dataURL(CFDataRef data, const String&amp; mimeType)
</ins><span class="cx"> {
</span><del>-    auto uti = utiFromMIMEType(mimeType);
-    ASSERT(uti);
-
-    auto data = adoptCF(CFDataCreateMutable(kCFAllocatorDefault, 0));
-    if (!encodeImage(image, uti.get(), quality, data.get()))
-        return ASCIILiteral(&quot;data:,&quot;);
-
</del><span class="cx">     Vector&lt;char&gt; base64Data;
</span><del>-    base64Encode(CFDataGetBytePtr(data.get()), CFDataGetLength(data.get()), base64Data);
</del><ins>+    base64Encode(CFDataGetBytePtr(data), CFDataGetLength(data), base64Data);
</ins><span class="cx"> 
</span><span class="cx">     return &quot;data:&quot; + mimeType + &quot;;base64,&quot; + base64Data;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-static Vector&lt;uint8_t&gt; data(CGImageRef image, const String&amp; mimeType, std::optional&lt;double&gt; quality)
</del><ins>+static Vector&lt;uint8_t&gt; dataVector(CFDataRef cfData)
</ins><span class="cx"> {
</span><del>-    auto uti = utiFromMIMEType(mimeType);
-    ASSERT(uti);
-
-    auto cfData = adoptCF(CFDataCreateMutable(kCFAllocatorDefault, 0));
-    if (!encodeImage(image, uti.get(), quality, cfData.get()))
-        return { };
-
</del><span class="cx">     Vector&lt;uint8_t&gt; data;
</span><del>-    data.append(CFDataGetBytePtr(cfData.get()), CFDataGetLength(cfData.get()));
</del><ins>+    data.append(CFDataGetBytePtr(cfData), CFDataGetLength(cfData));
</ins><span class="cx">     return data;
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> String ImageBuffer::toDataURL(const String&amp; mimeType, std::optional&lt;double&gt; quality, CoordinateSystem) const
</span><span class="cx"> {
</span><del>-    if (auto image = toCGImage(mimeType))
-        return dataURL(image.get(), mimeType, quality);
</del><ins>+    if (auto data = toCFData(mimeType, quality))
+        return dataURL(data.get(), mimeType);
</ins><span class="cx">     return ASCIILiteral(&quot;data:,&quot;);
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> Vector&lt;uint8_t&gt; ImageBuffer::toData(const String&amp; mimeType, std::optional&lt;double&gt; quality) const
</span><span class="cx"> {
</span><del>-    if (auto image = toCGImage(mimeType))
-        return data(image.get(), mimeType, quality);
</del><ins>+    if (auto data = toCFData(mimeType, quality))
+        return dataVector(data.get());
</ins><span class="cx">     return { };
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-RetainPtr&lt;CGImageRef&gt; ImageBuffer::toCGImage(const String&amp; mimeType) const
</del><ins>+RetainPtr&lt;CFDataRef&gt; ImageBuffer::toCFData(const String&amp; mimeType, std::optional&lt;double&gt; quality) const
</ins><span class="cx"> {
</span><span class="cx">     ASSERT(MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType));
</span><span class="cx"> 
</span><span class="lines">@@ -537,19 +523,16 @@
</span><span class="cx">     ASSERT(uti);
</span><span class="cx"> 
</span><span class="cx">     RetainPtr&lt;CGImageRef&gt; image;
</span><ins>+    RefPtr&lt;Uint8ClampedArray&gt; premultipliedData;
</ins><span class="cx"> 
</span><span class="cx">     if (CFEqual(uti.get(), jpegUTI())) {
</span><span class="cx">         // JPEGs don't have an alpha channel, so we have to manually composite on top of black.
</span><del>-        // premultipliedData is leaked here, then adopted in the releaseData lambda passed to CGDataProviderCreateWithData().
-        Uint8ClampedArray* premultipliedData = getPremultipliedImageData(IntRect(IntPoint(0, 0), logicalSize())).leakRef();
</del><ins>+        premultipliedData = getPremultipliedImageData(IntRect(IntPoint(), logicalSize()));
</ins><span class="cx">         if (!premultipliedData)
</span><span class="cx">             return nullptr;
</span><span class="cx"> 
</span><span class="cx">         size_t dataSize = 4 * logicalSize().width() * logicalSize().height();
</span><del>-        auto dataProvider = adoptCF(CGDataProviderCreateWithData(premultipliedData, premultipliedData-&gt;data(), dataSize, [](void* info, const void*, size_t) {
-            ASSERT(info);
-            adoptRef(reinterpret_cast&lt;Uint8ClampedArray*&gt;(info));
-        }));
</del><ins>+        auto dataProvider = adoptCF(CGDataProviderCreateWithData(nullptr, premultipliedData-&gt;data(), dataSize, nullptr));
</ins><span class="cx">         if (!dataProvider)
</span><span class="cx">             return nullptr;
</span><span class="cx"> 
</span><span class="lines">@@ -567,10 +550,14 @@
</span><span class="cx">         image = adoptCF(CGBitmapContextCreateImage(context.get()));
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-    return image;
</del><ins>+    auto cfData = adoptCF(CFDataCreateMutable(kCFAllocatorDefault, 0));
+    if (!encodeImage(image.get(), uti.get(), quality, cfData.get()))
+        return nullptr;
+
+    return WTFMove(cfData);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-static RetainPtr&lt;CGImageRef&gt; cgImage(const ImageData&amp; source, const String&amp; mimeType)
</del><ins>+static RetainPtr&lt;CFDataRef&gt; cfData(const ImageData&amp; source, const String&amp; mimeType, std::optional&lt;double&gt; quality)
</ins><span class="cx"> {
</span><span class="cx">     ASSERT(MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType));
</span><span class="cx"> 
</span><span class="lines">@@ -610,20 +597,26 @@
</span><span class="cx">     if (!dataProvider)
</span><span class="cx">         return nullptr;
</span><span class="cx"> 
</span><del>-    return adoptCF(CGImageCreate(source.width(), source.height(), 8, 32, 4 * source.width(), sRGBColorSpaceRef(), kCGBitmapByteOrderDefault | dataAlphaInfo, dataProvider.get(), 0, false, kCGRenderingIntentDefault));
</del><ins>+    auto image = adoptCF(CGImageCreate(source.width(), source.height(), 8, 32, 4 * source.width(), sRGBColorSpaceRef(), kCGBitmapByteOrderDefault | dataAlphaInfo, dataProvider.get(), 0, false, kCGRenderingIntentDefault));
+
+    auto cfData = adoptCF(CFDataCreateMutable(kCFAllocatorDefault, 0));
+    if (!encodeImage(image.get(), uti.get(), quality, cfData.get()))
+        return nullptr;
+
+    return WTFMove(cfData);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> String dataURL(const ImageData&amp; source, const String&amp; mimeType, std::optional&lt;double&gt; quality)
</span><span class="cx"> {
</span><del>-    if (auto image = cgImage(source, mimeType))
-        return dataURL(image.get(), mimeType, quality);
</del><ins>+    if (auto data = cfData(source, mimeType, quality))
+        return dataURL(data.get(), mimeType);
</ins><span class="cx">     return ASCIILiteral(&quot;data:,&quot;);
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> Vector&lt;uint8_t&gt; data(const ImageData&amp; source, const String&amp; mimeType, std::optional&lt;double&gt; quality)
</span><span class="cx"> {
</span><del>-    if (auto image = cgImage(source, mimeType))
-        return data(image.get(), mimeType, quality);
</del><ins>+    if (auto data = cfData(source, mimeType, quality))
+        return dataVector(data.get());
</ins><span class="cx">     return { };
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>