<!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>[183847] trunk/Source/JavaScriptCore</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/183847">183847</a></dd>
<dt>Author</dt> <dd>fpizlo@apple.com</dd>
<dt>Date</dt> <dd>2015-05-05 20:39:20 -0700 (Tue, 05 May 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>GC has trouble with pathologically large array allocations
https://bugs.webkit.org/show_bug.cgi?id=144609

Reviewed by Mark Lam.

* heap/Heap.cpp:
(JSC::Heap::updateObjectCounts): Make this code less confusing.
* heap/SlotVisitorInlines.h:
(JSC::SlotVisitor::copyLater): The early return for isOversize() was the bug. We still need to report these bytes as live. Otherwise the GC doesn't know that it owns this memory.
* jsc.cpp: Add size measuring hooks to write the largeish test.
(GlobalObject::finishCreation):
(functionGCAndSweep):
(functionFullGC):
(functionEdenGC):
(functionHeapSize):
* tests/stress/new-array-storage-array-with-size.js: Fix this so that it actually allocates ArrayStorage arrays and tests the thing it was supposed to test.
* tests/stress/new-largeish-contiguous-array-with-size.js: Added. This tests what the other test accidentally started testing, but does so without running your system out of memory.
(foo):
(test):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCoreheapHeapcpp">trunk/Source/JavaScriptCore/heap/Heap.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreheapSlotVisitorInlinesh">trunk/Source/JavaScriptCore/heap/SlotVisitorInlines.h</a></li>
<li><a href="#trunkSourceJavaScriptCorejsccpp">trunk/Source/JavaScriptCore/jsc.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoretestsstressnewarraystoragearraywithsizejs">trunk/Source/JavaScriptCore/tests/stress/new-array-storage-array-with-size.js</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoretestsstressnewlargeishcontiguousarraywithsizejs">trunk/Source/JavaScriptCore/tests/stress/new-largeish-contiguous-array-with-size.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (183846 => 183847)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2015-05-06 02:10:43 UTC (rev 183846)
+++ trunk/Source/JavaScriptCore/ChangeLog        2015-05-06 03:39:20 UTC (rev 183847)
</span><span class="lines">@@ -1,5 +1,27 @@
</span><span class="cx"> 2015-05-05  Filip Pizlo  &lt;fpizlo@apple.com&gt;
</span><span class="cx"> 
</span><ins>+        GC has trouble with pathologically large array allocations
+        https://bugs.webkit.org/show_bug.cgi?id=144609
+
+        Reviewed by Mark Lam.
+
+        * heap/Heap.cpp:
+        (JSC::Heap::updateObjectCounts): Make this code less confusing.
+        * heap/SlotVisitorInlines.h:
+        (JSC::SlotVisitor::copyLater): The early return for isOversize() was the bug. We still need to report these bytes as live. Otherwise the GC doesn't know that it owns this memory.
+        * jsc.cpp: Add size measuring hooks to write the largeish test.
+        (GlobalObject::finishCreation):
+        (functionGCAndSweep):
+        (functionFullGC):
+        (functionEdenGC):
+        (functionHeapSize):
+        * tests/stress/new-array-storage-array-with-size.js: Fix this so that it actually allocates ArrayStorage arrays and tests the thing it was supposed to test.
+        * tests/stress/new-largeish-contiguous-array-with-size.js: Added. This tests what the other test accidentally started testing, but does so without running your system out of memory.
+        (foo):
+        (test):
+
+2015-05-05  Filip Pizlo  &lt;fpizlo@apple.com&gt;
+
</ins><span class="cx">         FTL SwitchString slow case creates duplicate switch cases
</span><span class="cx">         https://bugs.webkit.org/show_bug.cgi?id=144634
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreheapHeapcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/heap/Heap.cpp (183846 => 183847)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/heap/Heap.cpp        2015-05-06 02:10:43 UTC (rev 183846)
+++ trunk/Source/JavaScriptCore/heap/Heap.cpp        2015-05-06 03:39:20 UTC (rev 183847)
</span><span class="lines">@@ -816,15 +816,14 @@
</span><span class="cx"> #endif
</span><span class="cx">         dataLogF(&quot;\nNumber of live Objects after GC %lu, took %.6f secs\n&quot;, static_cast&lt;unsigned long&gt;(visitCount), WTF::monotonicallyIncreasingTime() - gcStartTime);
</span><span class="cx">     }
</span><del>-
-    if (m_operationInProgress == EdenCollection) {
-        m_totalBytesVisited += m_slotVisitor.bytesVisited();
-        m_totalBytesCopied += m_slotVisitor.bytesCopied();
-    } else {
-        ASSERT(m_operationInProgress == FullCollection);
-        m_totalBytesVisited = m_slotVisitor.bytesVisited();
-        m_totalBytesCopied = m_slotVisitor.bytesCopied();
</del><ins>+    
+    if (m_operationInProgress == FullCollection) {
+        m_totalBytesVisited = 0;
+        m_totalBytesCopied = 0;
</ins><span class="cx">     }
</span><ins>+
+    m_totalBytesVisited += m_slotVisitor.bytesVisited();
+    m_totalBytesCopied += m_slotVisitor.bytesCopied();
</ins><span class="cx"> #if ENABLE(PARALLEL_GC)
</span><span class="cx">     m_totalBytesVisited += m_sharedData.childBytesVisited();
</span><span class="cx">     m_totalBytesCopied += m_sharedData.childBytesCopied();
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreheapSlotVisitorInlinesh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/heap/SlotVisitorInlines.h (183846 => 183847)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/heap/SlotVisitorInlines.h        2015-05-06 02:10:43 UTC (rev 183846)
+++ trunk/Source/JavaScriptCore/heap/SlotVisitorInlines.h        2015-05-06 03:39:20 UTC (rev 183847)
</span><span class="lines">@@ -238,10 +238,8 @@
</span><span class="cx"> {
</span><span class="cx">     ASSERT(bytes);
</span><span class="cx">     CopiedBlock* block = CopiedSpace::blockFor(ptr);
</span><del>-    if (block-&gt;isOversize()) {
</del><ins>+    if (block-&gt;isOversize())
</ins><span class="cx">         m_shared.m_copiedSpace-&gt;pin(block);
</span><del>-        return;
-    }
</del><span class="cx"> 
</span><span class="cx">     ASSERT(heap()-&gt;m_storageSpace.contains(block));
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorejsccpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/jsc.cpp (183846 => 183847)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/jsc.cpp        2015-05-06 02:10:43 UTC (rev 183846)
+++ trunk/Source/JavaScriptCore/jsc.cpp        2015-05-06 03:39:20 UTC (rev 183847)
</span><span class="lines">@@ -447,6 +447,7 @@
</span><span class="cx"> static EncodedJSValue JSC_HOST_CALL functionGCAndSweep(ExecState*);
</span><span class="cx"> static EncodedJSValue JSC_HOST_CALL functionFullGC(ExecState*);
</span><span class="cx"> static EncodedJSValue JSC_HOST_CALL functionEdenGC(ExecState*);
</span><ins>+static EncodedJSValue JSC_HOST_CALL functionHeapSize(ExecState*);
</ins><span class="cx"> static EncodedJSValue JSC_HOST_CALL functionDeleteAllCompiledCode(ExecState*);
</span><span class="cx"> #ifndef NDEBUG
</span><span class="cx"> static EncodedJSValue JSC_HOST_CALL functionReleaseExecutableMemory(ExecState*);
</span><span class="lines">@@ -585,6 +586,7 @@
</span><span class="cx">         addFunction(vm, &quot;gc&quot;, functionGCAndSweep, 0);
</span><span class="cx">         addFunction(vm, &quot;fullGC&quot;, functionFullGC, 0);
</span><span class="cx">         addFunction(vm, &quot;edenGC&quot;, functionEdenGC, 0);
</span><ins>+        addFunction(vm, &quot;gcHeapSize&quot;, functionHeapSize, 0);
</ins><span class="cx">         addFunction(vm, &quot;deleteAllCompiledCode&quot;, functionDeleteAllCompiledCode, 0);
</span><span class="cx"> #ifndef NDEBUG
</span><span class="cx">         addFunction(vm, &quot;dumpCallFrame&quot;, functionDumpCallFrame, 0);
</span><span class="lines">@@ -831,23 +833,29 @@
</span><span class="cx"> {
</span><span class="cx">     JSLockHolder lock(exec);
</span><span class="cx">     exec-&gt;heap()-&gt;collectAllGarbage();
</span><del>-    return JSValue::encode(jsUndefined());
</del><ins>+    return JSValue::encode(jsNumber(exec-&gt;heap()-&gt;sizeAfterLastFullCollection()));
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> EncodedJSValue JSC_HOST_CALL functionFullGC(ExecState* exec)
</span><span class="cx"> {
</span><span class="cx">     JSLockHolder lock(exec);
</span><span class="cx">     exec-&gt;heap()-&gt;collect(FullCollection);
</span><del>-    return JSValue::encode(jsUndefined());
</del><ins>+    return JSValue::encode(jsNumber(exec-&gt;heap()-&gt;sizeAfterLastFullCollection()));
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> EncodedJSValue JSC_HOST_CALL functionEdenGC(ExecState* exec)
</span><span class="cx"> {
</span><span class="cx">     JSLockHolder lock(exec);
</span><span class="cx">     exec-&gt;heap()-&gt;collect(EdenCollection);
</span><del>-    return JSValue::encode(jsUndefined());
</del><ins>+    return JSValue::encode(jsNumber(exec-&gt;heap()-&gt;sizeAfterLastEdenCollection()));
</ins><span class="cx"> }
</span><span class="cx"> 
</span><ins>+EncodedJSValue JSC_HOST_CALL functionHeapSize(ExecState* exec)
+{
+    JSLockHolder lock(exec);
+    return JSValue::encode(jsNumber(exec-&gt;heap()-&gt;size()));
+}
+
</ins><span class="cx"> EncodedJSValue JSC_HOST_CALL functionDeleteAllCompiledCode(ExecState* exec)
</span><span class="cx"> {
</span><span class="cx">     JSLockHolder lock(exec);
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoretestsstressnewarraystoragearraywithsizejs"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/tests/stress/new-array-storage-array-with-size.js (183846 => 183847)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/tests/stress/new-array-storage-array-with-size.js        2015-05-06 02:10:43 UTC (rev 183846)
+++ trunk/Source/JavaScriptCore/tests/stress/new-array-storage-array-with-size.js        2015-05-06 03:39:20 UTC (rev 183847)
</span><span class="lines">@@ -1,12 +1,15 @@
</span><del>-// https://bugs.webkit.org/show_bug.cgi?id=144609
-//@ skip
-
</del><span class="cx"> function foo(x) {
</span><span class="cx">     return new Array(x);
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> noInline(foo);
</span><span class="cx"> 
</span><ins>+// Warm up up to create array storage.
+for (var i = 0; i &lt; 10000; ++i) {
+    var array = foo(10);
+    array.__defineSetter__(0, function(v) { });
+}
+
</ins><span class="cx"> function test(size) {
</span><span class="cx">     var result = foo(size);
</span><span class="cx">     if (result.length != size)
</span><span class="lines">@@ -22,5 +25,5 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> for (var i = 0; i &lt; 100000; ++i) {
</span><del>-    test(1000000);
</del><ins>+    test(10);
</ins><span class="cx"> }
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoretestsstressnewlargeishcontiguousarraywithsizejs"></a>
<div class="addfile"><h4>Added: trunk/Source/JavaScriptCore/tests/stress/new-largeish-contiguous-array-with-size.js (0 => 183847)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/tests/stress/new-largeish-contiguous-array-with-size.js                                (rev 0)
+++ trunk/Source/JavaScriptCore/tests/stress/new-largeish-contiguous-array-with-size.js        2015-05-06 03:39:20 UTC (rev 183847)
</span><span class="lines">@@ -0,0 +1,45 @@
</span><ins>+// We only need one run of this with any GC or JIT strategy. This test is not particularly fast.
+// Unfortunately, it needs to run for a while to test the thing it's testing.
+//@ slow!
+//@ runDefault
+
+function foo(x) {
+    return new Array(x);
+}
+
+noInline(foo);
+
+function test(size) {
+    var result = foo(size);
+    if (result.length != size)
+        throw &quot;Error: bad result: &quot; + result;
+    var sawThings = false;
+    for (var s in result)
+        sawThings = true;
+    if (sawThings)
+        throw &quot;Error: array is in bad state: &quot; + result;
+    result[0] = &quot;42.5&quot;;
+    if (result[0] != &quot;42.5&quot;)
+        throw &quot;Error: array is in weird state: &quot; + result;
+}
+
+for (var i = 0; i &lt; 40000; ++i) {
+    // The test was written when we found that large array allocations weren't being accounted for
+    // in that part of the GC's accounting that determined the GC trigger. Consequently, the GC
+    // would run too infrequently in this loop and we would use an absurd amount of memory when this
+    // loop exited.
+    test(50000);
+}
+
+// Last time I tested, the heap should be 52520914 before and 50120146 after. I don't want to
+// enforce exactly that. If you regress the accounting code, the GC heap size at this point will
+// be much more than that.
+var result = gcHeapSize();
+if (result &gt; 100000000)
+    throw &quot;Error: heap too big before forced GC: &quot; + result;
+
+// Do a final check after GC, just for sanity.
+gc();
+result = gcHeapSize();
+if (result &gt; 100000000)
+    throw &quot;Error: heap too big after forced GC: &quot; + result;
</ins></span></pre>
</div>
</div>

</body>
</html>