<!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>[202173] trunk/Source/WTF</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/202173">202173</a></dd>
<dt>Author</dt> <dd>mark.lam@apple.com</dd>
<dt>Date</dt> <dd>2016-06-17 12:22:02 -0700 (Fri, 17 Jun 2016)</dd>
</dl>

<h3>Log Message</h3>
<pre>OOM Assertion failure in JSON.stringify.
https://bugs.webkit.org/show_bug.cgi?id=158794
&lt;rdar://problem/26826254&gt;

Reviewed by Saam Barati.

The bug was actually in StringBuilder::appendQuotedJSONString() where it failed
to detect an imminent unsigned int overflow.  The fix is to use Checked&lt;unsigned&gt;
for the needed math, and RELEASE_ASSERT afterwards that we did not overflow.

I also added more assertions to detect sooner if any there are any problems with
StringBuilder's m_buffer or m_length being incorrectly sized.  These assertions
have been run on the JSC and layout tests without any issue.

* wtf/text/StringBuilder.cpp:
(WTF::StringBuilder::resize):
(WTF::StringBuilder::allocateBuffer):
(WTF::StringBuilder::allocateBufferUpConvert):
(WTF::StringBuilder::reallocateBuffer&lt;LChar&gt;):
(WTF::StringBuilder::reallocateBuffer&lt;UChar&gt;):
(WTF::StringBuilder::reserveCapacity):
(WTF::StringBuilder::appendUninitializedSlow):
(WTF::StringBuilder::append):
(WTF::StringBuilder::appendQuotedJSONString):
* wtf/text/StringBuilder.h:
(WTF::StringBuilder::swap):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWTFChangeLog">trunk/Source/WTF/ChangeLog</a></li>
<li><a href="#trunkSourceWTFwtftextStringBuildercpp">trunk/Source/WTF/wtf/text/StringBuilder.cpp</a></li>
<li><a href="#trunkSourceWTFwtftextStringBuilderh">trunk/Source/WTF/wtf/text/StringBuilder.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWTFChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/ChangeLog (202172 => 202173)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/ChangeLog        2016-06-17 19:10:28 UTC (rev 202172)
+++ trunk/Source/WTF/ChangeLog        2016-06-17 19:22:02 UTC (rev 202173)
</span><span class="lines">@@ -1,3 +1,32 @@
</span><ins>+2016-06-17  Mark Lam  &lt;mark.lam@apple.com&gt;
+
+        OOM Assertion failure in JSON.stringify.
+        https://bugs.webkit.org/show_bug.cgi?id=158794
+        &lt;rdar://problem/26826254&gt;
+
+        Reviewed by Saam Barati.
+
+        The bug was actually in StringBuilder::appendQuotedJSONString() where it failed
+        to detect an imminent unsigned int overflow.  The fix is to use Checked&lt;unsigned&gt;
+        for the needed math, and RELEASE_ASSERT afterwards that we did not overflow.
+
+        I also added more assertions to detect sooner if any there are any problems with
+        StringBuilder's m_buffer or m_length being incorrectly sized.  These assertions
+        have been run on the JSC and layout tests without any issue.
+
+        * wtf/text/StringBuilder.cpp:
+        (WTF::StringBuilder::resize):
+        (WTF::StringBuilder::allocateBuffer):
+        (WTF::StringBuilder::allocateBufferUpConvert):
+        (WTF::StringBuilder::reallocateBuffer&lt;LChar&gt;):
+        (WTF::StringBuilder::reallocateBuffer&lt;UChar&gt;):
+        (WTF::StringBuilder::reserveCapacity):
+        (WTF::StringBuilder::appendUninitializedSlow):
+        (WTF::StringBuilder::append):
+        (WTF::StringBuilder::appendQuotedJSONString):
+        * wtf/text/StringBuilder.h:
+        (WTF::StringBuilder::swap):
+
</ins><span class="cx"> 2016-06-14  Filip Pizlo  &lt;fpizlo@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Baseline JIT should be concurrent
</span></span></pre></div>
<a id="trunkSourceWTFwtftextStringBuildercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/text/StringBuilder.cpp (202172 => 202173)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/text/StringBuilder.cpp        2016-06-17 19:10:28 UTC (rev 202172)
+++ trunk/Source/WTF/wtf/text/StringBuilder.cpp        2016-06-17 19:22:02 UTC (rev 202173)
</span><span class="lines">@@ -1,5 +1,5 @@
</span><span class="cx"> /*
</span><del>- * Copyright (C) 2010, 2013 Apple Inc. All rights reserved.
</del><ins>+ * Copyright (C) 2010, 2013, 2016 Apple Inc. All rights reserved.
</ins><span class="cx">  * Copyright (C) 2012 Google Inc. All rights reserved.
</span><span class="cx">  *
</span><span class="cx">  * Redistribution and use in source and binary forms, with or without
</span><span class="lines">@@ -80,6 +80,7 @@
</span><span class="cx">                 allocateBuffer(m_buffer-&gt;characters16(), m_buffer-&gt;length());
</span><span class="cx">         }
</span><span class="cx">         m_length = newSize;
</span><ins>+        ASSERT(m_buffer-&gt;length() &gt;= m_length);
</ins><span class="cx">         return;
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="lines">@@ -103,6 +104,7 @@
</span><span class="cx">     // Update the builder state.
</span><span class="cx">     m_buffer = WTFMove(buffer);
</span><span class="cx">     m_string = String();
</span><ins>+    ASSERT(m_buffer-&gt;length() == requiredLength);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> // Allocate a new 16 bit buffer, copying in currentCharacters (these may come from either m_string
</span><span class="lines">@@ -117,6 +119,7 @@
</span><span class="cx">     // Update the builder state.
</span><span class="cx">     m_buffer = WTFMove(buffer);
</span><span class="cx">     m_string = String();
</span><ins>+    ASSERT(m_buffer-&gt;length() == requiredLength);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> // Allocate a new 16 bit buffer, copying in currentCharacters (which is 8 bit and may come
</span><span class="lines">@@ -124,6 +127,7 @@
</span><span class="cx"> void StringBuilder::allocateBufferUpConvert(const LChar* currentCharacters, unsigned requiredLength)
</span><span class="cx"> {
</span><span class="cx">     ASSERT(m_is8Bit);
</span><ins>+    ASSERT(requiredLength &gt;= m_length);
</ins><span class="cx">     // Copy the existing data into a new buffer, set result to point to the end of the existing data.
</span><span class="cx">     auto buffer = StringImpl::createUninitialized(requiredLength, m_bufferCharacters16);
</span><span class="cx">     for (unsigned i = 0; i &lt; m_length; ++i)
</span><span class="lines">@@ -134,6 +138,7 @@
</span><span class="cx">     // Update the builder state.
</span><span class="cx">     m_buffer = WTFMove(buffer);
</span><span class="cx">     m_string = String();
</span><ins>+    ASSERT(m_buffer-&gt;length() == requiredLength);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> template &lt;&gt;
</span><span class="lines">@@ -150,6 +155,7 @@
</span><span class="cx">         m_buffer = StringImpl::reallocate(m_buffer.releaseNonNull(), requiredLength, m_bufferCharacters8);
</span><span class="cx">     else
</span><span class="cx">         allocateBuffer(m_buffer-&gt;characters8(), requiredLength);
</span><ins>+    ASSERT(m_buffer-&gt;length() == requiredLength);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> template &lt;&gt;
</span><span class="lines">@@ -165,6 +171,7 @@
</span><span class="cx">         m_buffer = StringImpl::reallocate(m_buffer.releaseNonNull(), requiredLength, m_bufferCharacters16);
</span><span class="cx">     else
</span><span class="cx">         allocateBuffer(m_buffer-&gt;characters16(), requiredLength);
</span><ins>+    ASSERT(m_buffer-&gt;length() == requiredLength);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void StringBuilder::reserveCapacity(unsigned newCapacity)
</span><span class="lines">@@ -189,6 +196,7 @@
</span><span class="cx">                 allocateBuffer(m_string.characters16(), newCapacity);
</span><span class="cx">         }
</span><span class="cx">     }
</span><ins>+    ASSERT(!newCapacity || m_buffer-&gt;length() &gt;= newCapacity);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> // Make 'length' additional capacity be available in m_buffer, update m_string &amp; m_length,
</span><span class="lines">@@ -234,6 +242,7 @@
</span><span class="cx">     
</span><span class="cx">     CharType* result = getBufferCharacters&lt;CharType&gt;() + m_length;
</span><span class="cx">     m_length = requiredLength;
</span><ins>+    ASSERT(m_buffer-&gt;length() &gt;= m_length);
</ins><span class="cx">     return result;
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -267,10 +276,11 @@
</span><span class="cx">             allocateBufferUpConvert(m_string.isNull() ? 0 : m_string.characters8(), expandedCapacity(capacity(), requiredLength));
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        memcpy(m_bufferCharacters16 + m_length, characters, static_cast&lt;size_t&gt;(length) * sizeof(UChar));        
</del><ins>+        memcpy(m_bufferCharacters16 + m_length, characters, static_cast&lt;size_t&gt;(length) * sizeof(UChar));
</ins><span class="cx">         m_length = requiredLength;
</span><span class="cx">     } else
</span><span class="cx">         memcpy(appendUninitialized&lt;UChar&gt;(length), characters, static_cast&lt;size_t&gt;(length) * sizeof(UChar));
</span><ins>+    ASSERT(m_buffer-&gt;length() &gt;= m_length);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void StringBuilder::append(const LChar* characters, unsigned length)
</span><span class="lines">@@ -412,9 +422,10 @@
</span><span class="cx">     // to worry about reallocating in the middle.
</span><span class="cx">     // The 2 is for the '&quot;' quotes on each end.
</span><span class="cx">     // The 6 is for characters that need to be \uNNNN encoded.
</span><del>-    size_t maximumCapacityRequired = length() + 2 + string.length() * 6;
-    RELEASE_ASSERT(maximumCapacityRequired &lt; std::numeric_limits&lt;unsigned&gt;::max());
-    unsigned allocationSize = maximumCapacityRequired;
</del><ins>+    Checked&lt;unsigned&gt; stringLength = string.length();
+    Checked&lt;unsigned&gt; maximumCapacityRequired = length();
+    maximumCapacityRequired += 2 + stringLength * 6;
+    unsigned allocationSize = maximumCapacityRequired.unsafeGet();
</ins><span class="cx">     // This max() is here to allow us to allocate sizes between the range [2^31, 2^32 - 2] because roundUpToPowerOfTwo(1&lt;&lt;31 + some int smaller than 1&lt;&lt;31) == 0.
</span><span class="cx">     allocationSize = std::max(allocationSize, roundUpToPowerOfTwo(allocationSize));
</span><span class="cx"> 
</span><span class="lines">@@ -422,6 +433,7 @@
</span><span class="cx">         allocateBufferUpConvert(m_bufferCharacters8, allocationSize);
</span><span class="cx">     else
</span><span class="cx">         reserveCapacity(allocationSize);
</span><ins>+    ASSERT(m_buffer-&gt;length() &gt;= allocationSize);
</ins><span class="cx"> 
</span><span class="cx">     if (is8Bit()) {
</span><span class="cx">         ASSERT(string.is8Bit());
</span><span class="lines">@@ -440,6 +452,7 @@
</span><span class="cx">         *output++ = '&quot;';
</span><span class="cx">         m_length = output - m_bufferCharacters16;
</span><span class="cx">     }
</span><ins>+    ASSERT(m_buffer-&gt;length() &gt;= m_length);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> } // namespace WTF
</span></span></pre></div>
<a id="trunkSourceWTFwtftextStringBuilderh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/text/StringBuilder.h (202172 => 202173)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/text/StringBuilder.h        2016-06-17 19:10:28 UTC (rev 202172)
+++ trunk/Source/WTF/wtf/text/StringBuilder.h        2016-06-17 19:22:02 UTC (rev 202173)
</span><span class="lines">@@ -1,5 +1,5 @@
</span><span class="cx"> /*
</span><del>- * Copyright (C) 2009, 2010, 2012, 2013 Apple Inc. All rights reserved.
</del><ins>+ * Copyright (C) 2009-2010, 2012-2013, 2016 Apple Inc. All rights reserved.
</ins><span class="cx">  * Copyright (C) 2012 Google Inc. All rights reserved.
</span><span class="cx">  *
</span><span class="cx">  * Redistribution and use in source and binary forms, with or without
</span><span class="lines">@@ -276,6 +276,7 @@
</span><span class="cx">         m_buffer.swap(stringBuilder.m_buffer);
</span><span class="cx">         std::swap(m_is8Bit, stringBuilder.m_is8Bit);
</span><span class="cx">         std::swap(m_bufferCharacters8, stringBuilder.m_bufferCharacters8);
</span><ins>+        ASSERT(!m_buffer || m_buffer-&gt;length() &gt;= m_length);
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx"> private:
</span></span></pre>
</div>
</div>

</body>
</html>