<!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>[170677] 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/170677">170677</a></dd>
<dt>Author</dt> <dd>mark.lam@apple.com</dd>
<dt>Date</dt> <dd>2014-07-01 16:40:32 -0700 (Tue, 01 Jul 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Debugger's breakpoint list should not be a Vector.
&lt;https://webkit.org/b/134514&gt;

Reviewed by Geoffrey Garen.

The debugger currently stores breakpoint data as entries in a Vector (see
BreakpointsInLine).  It also keeps a fast map look up of breakpoint IDs to
the breakpoint data (see m_breakpointIDToBreakpoint).  Because a Vector can
compact or reallocate its backing store, this can causes all sorts of havoc.
The m_breakpointIDToBreakpoint map assumes that the breakpoint data doesn't
move in memory.

The fix is to replace the BreakpointsInLine Vector with a BreakpointsList
doubly linked list.

* debugger/Breakpoint.h:
(JSC::Breakpoint::Breakpoint):
(JSC::BreakpointsList::~BreakpointsList):
* debugger/Debugger.cpp:
(JSC::Debugger::setBreakpoint):
(JSC::Debugger::removeBreakpoint):
(JSC::Debugger::hasBreakpoint):
* debugger/Debugger.h:</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCoredebuggerBreakpointh">trunk/Source/JavaScriptCore/debugger/Breakpoint.h</a></li>
<li><a href="#trunkSourceJavaScriptCoredebuggerDebuggercpp">trunk/Source/JavaScriptCore/debugger/Debugger.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoredebuggerDebuggerh">trunk/Source/JavaScriptCore/debugger/Debugger.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (170676 => 170677)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2014-07-01 23:40:06 UTC (rev 170676)
+++ trunk/Source/JavaScriptCore/ChangeLog        2014-07-01 23:40:32 UTC (rev 170677)
</span><span class="lines">@@ -1,3 +1,29 @@
</span><ins>+2014-07-01  Mark Lam  &lt;mark.lam@apple.com&gt;
+
+        Debugger's breakpoint list should not be a Vector.
+        &lt;https://webkit.org/b/134514&gt;
+
+        Reviewed by Geoffrey Garen.
+
+        The debugger currently stores breakpoint data as entries in a Vector (see
+        BreakpointsInLine).  It also keeps a fast map look up of breakpoint IDs to
+        the breakpoint data (see m_breakpointIDToBreakpoint).  Because a Vector can
+        compact or reallocate its backing store, this can causes all sorts of havoc.
+        The m_breakpointIDToBreakpoint map assumes that the breakpoint data doesn't
+        move in memory.
+
+        The fix is to replace the BreakpointsInLine Vector with a BreakpointsList
+        doubly linked list.
+
+        * debugger/Breakpoint.h:
+        (JSC::Breakpoint::Breakpoint):
+        (JSC::BreakpointsList::~BreakpointsList):
+        * debugger/Debugger.cpp:
+        (JSC::Debugger::setBreakpoint):
+        (JSC::Debugger::removeBreakpoint):
+        (JSC::Debugger::hasBreakpoint):
+        * debugger/Debugger.h:
+
</ins><span class="cx"> 2014-06-30  Michael Saboff  &lt;msaboff@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Add option to run-jsc-stress-testes to filter out tests that use large heaps
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoredebuggerBreakpointh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/debugger/Breakpoint.h (170676 => 170677)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/debugger/Breakpoint.h        2014-07-01 23:40:06 UTC (rev 170676)
+++ trunk/Source/JavaScriptCore/debugger/Breakpoint.h        2014-07-01 23:40:32 UTC (rev 170677)
</span><span class="lines">@@ -27,11 +27,13 @@
</span><span class="cx"> #define Breakpoint_h
</span><span class="cx"> 
</span><span class="cx"> #include &quot;DebuggerPrimitives.h&quot;
</span><ins>+#include &lt;wtf/DoublyLinkedList.h&gt;
+#include &lt;wtf/RefCounted.h&gt;
</ins><span class="cx"> #include &lt;wtf/text/WTFString.h&gt;
</span><span class="cx"> 
</span><span class="cx"> namespace JSC {
</span><span class="cx"> 
</span><del>-struct Breakpoint {
</del><ins>+struct Breakpoint : public DoublyLinkedListNode&lt;Breakpoint&gt; {
</ins><span class="cx">     Breakpoint()
</span><span class="cx">         : id(noBreakpointID)
</span><span class="cx">         , sourceID(noSourceID)
</span><span class="lines">@@ -51,6 +53,16 @@
</span><span class="cx">     {
</span><span class="cx">     }
</span><span class="cx"> 
</span><ins>+    Breakpoint(const Breakpoint&amp; other)
+        : id(other.id)
+        , sourceID(other.sourceID)
+        , line(other.line)
+        , column(other.column)
+        , condition(other.condition)
+        , autoContinue(other.autoContinue)
+    {
+    }
+
</ins><span class="cx">     BreakpointID id;
</span><span class="cx">     SourceID sourceID;
</span><span class="cx">     unsigned line;
</span><span class="lines">@@ -59,8 +71,26 @@
</span><span class="cx">     bool autoContinue;
</span><span class="cx"> 
</span><span class="cx">     static const unsigned unspecifiedColumn = UINT_MAX;
</span><ins>+
+private:
+    Breakpoint* m_prev;
+    Breakpoint* m_next;
+
+    friend class WTF::DoublyLinkedListNode&lt;Breakpoint&gt;;
</ins><span class="cx"> };
</span><span class="cx"> 
</span><ins>+class BreakpointsList : public DoublyLinkedList&lt;Breakpoint&gt;,
+    public RefCounted&lt;BreakpointsList&gt; {
+public:
+    ~BreakpointsList()
+    {
+        Breakpoint* breakpoint;
+        while ((breakpoint = removeHead()))
+            delete breakpoint;
+        ASSERT(isEmpty());
+    }
+};
+
</ins><span class="cx"> } // namespace JSC
</span><span class="cx"> 
</span><span class="cx"> #endif // Breakpoint_h
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoredebuggerDebuggercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/debugger/Debugger.cpp (170676 => 170677)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/debugger/Debugger.cpp        2014-07-01 23:40:06 UTC (rev 170676)
+++ trunk/Source/JavaScriptCore/debugger/Debugger.cpp        2014-07-01 23:40:32 UTC (rev 170677)
</span><span class="lines">@@ -357,18 +357,18 @@
</span><span class="cx">         it = m_sourceIDToBreakpoints.set(sourceID, LineToBreakpointsMap()).iterator;
</span><span class="cx">     LineToBreakpointsMap::iterator breaksIt = it-&gt;value.find(line);
</span><span class="cx">     if (breaksIt == it-&gt;value.end())
</span><del>-        breaksIt = it-&gt;value.set(line, BreakpointsInLine()).iterator;
</del><ins>+        breaksIt = it-&gt;value.set(line, adoptRef(new BreakpointsList)).iterator;
</ins><span class="cx"> 
</span><del>-    BreakpointsInLine&amp; breakpoints = breaksIt-&gt;value;
-    unsigned breakpointsCount = breakpoints.size();
-    for (unsigned i = 0; i &lt; breakpointsCount; i++)
-        if (breakpoints[i].column == column) {
</del><ins>+    BreakpointsList&amp; breakpoints = *breaksIt-&gt;value;
+    for (Breakpoint* current = breakpoints.head(); current; current = current-&gt;next()) {
+        if (current-&gt;column == column) {
</ins><span class="cx">             // The breakpoint already exists. We're not allowed to create a new
</span><span class="cx">             // breakpoint at this location. Rather than returning the breakpointID
</span><span class="cx">             // of the pre-existing breakpoint, we need to return noBreakpointID
</span><span class="cx">             // to indicate that we're not creating a new one.
</span><span class="cx">             return noBreakpointID;
</span><span class="cx">         }
</span><ins>+    }
</ins><span class="cx"> 
</span><span class="cx">     BreakpointID id = ++m_topBreakpointID;
</span><span class="cx">     RELEASE_ASSERT(id != noBreakpointID);
</span><span class="lines">@@ -377,8 +377,9 @@
</span><span class="cx">     actualLine = line;
</span><span class="cx">     actualColumn = column;
</span><span class="cx"> 
</span><del>-    breakpoints.append(breakpoint);
-    m_breakpointIDToBreakpoint.set(id, &amp;breakpoints.last());
</del><ins>+    Breakpoint* newBreakpoint = new Breakpoint(breakpoint);
+    breakpoints.append(newBreakpoint);
+    m_breakpointIDToBreakpoint.set(id, newBreakpoint);
</ins><span class="cx"> 
</span><span class="cx">     toggleBreakpoint(breakpoint, BreakpointEnabled);
</span><span class="cx"> 
</span><span class="lines">@@ -391,31 +392,35 @@
</span><span class="cx"> 
</span><span class="cx">     BreakpointIDToBreakpointMap::iterator idIt = m_breakpointIDToBreakpoint.find(id);
</span><span class="cx">     ASSERT(idIt != m_breakpointIDToBreakpoint.end());
</span><del>-    Breakpoint&amp; breakpoint = *idIt-&gt;value;
</del><ins>+    Breakpoint* breakpoint = idIt-&gt;value;
</ins><span class="cx"> 
</span><del>-    SourceID sourceID = breakpoint.sourceID;
</del><ins>+    SourceID sourceID = breakpoint-&gt;sourceID;
</ins><span class="cx">     ASSERT(sourceID);
</span><span class="cx">     SourceIDToBreakpointsMap::iterator it = m_sourceIDToBreakpoints.find(sourceID);
</span><span class="cx">     ASSERT(it != m_sourceIDToBreakpoints.end());
</span><del>-    LineToBreakpointsMap::iterator breaksIt = it-&gt;value.find(breakpoint.line);
</del><ins>+    LineToBreakpointsMap::iterator breaksIt = it-&gt;value.find(breakpoint-&gt;line);
</ins><span class="cx">     ASSERT(breaksIt != it-&gt;value.end());
</span><span class="cx"> 
</span><del>-    toggleBreakpoint(breakpoint, BreakpointDisabled);
</del><ins>+    toggleBreakpoint(*breakpoint, BreakpointDisabled);
</ins><span class="cx"> 
</span><del>-    BreakpointsInLine&amp; breakpoints = breaksIt-&gt;value;
-    unsigned breakpointsCount = breakpoints.size();
-    for (unsigned i = 0; i &lt; breakpointsCount; i++) {
-        if (breakpoints[i].id == breakpoint.id) {
-            breakpoints.remove(i);
-            m_breakpointIDToBreakpoint.remove(idIt);
</del><ins>+    BreakpointsList&amp; breakpoints = *breaksIt-&gt;value;
+#if !ASSERT_DISABLED
+    bool found = false;
+    for (Breakpoint* current = breakpoints.head(); current &amp;&amp; !found; current = current-&gt;next()) {
+        if (current-&gt;id == breakpoint-&gt;id)
+            found = true;
+    }
+    ASSERT(found);
+#endif
</ins><span class="cx"> 
</span><del>-            if (breakpoints.isEmpty()) {
-                it-&gt;value.remove(breaksIt);
-                if (it-&gt;value.isEmpty())
-                    m_sourceIDToBreakpoints.remove(it);
-            }
-            break;
-        }
</del><ins>+    m_breakpointIDToBreakpoint.remove(idIt);
+    breakpoints.remove(breakpoint);
+    delete breakpoint;
+
+    if (breakpoints.isEmpty()) {
+        it-&gt;value.remove(breaksIt);
+        if (it-&gt;value.isEmpty())
+            m_sourceIDToBreakpoints.remove(it);
</ins><span class="cx">     }
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -436,12 +441,11 @@
</span><span class="cx">         return false;
</span><span class="cx"> 
</span><span class="cx">     bool hit = false;
</span><del>-    const BreakpointsInLine&amp; breakpoints = breaksIt-&gt;value;
-    unsigned breakpointsCount = breakpoints.size();
-    unsigned i;
-    for (i = 0; i &lt; breakpointsCount; i++) {
-        unsigned breakLine = breakpoints[i].line;
-        unsigned breakColumn = breakpoints[i].column;
</del><ins>+    const BreakpointsList&amp; breakpoints = *breaksIt-&gt;value;
+    Breakpoint* breakpoint;
+    for (breakpoint = breakpoints.head(); breakpoint; breakpoint = breakpoint-&gt;next()) {
+        unsigned breakLine = breakpoint-&gt;line;
+        unsigned breakColumn = breakpoint-&gt;column;
</ins><span class="cx">         // Since frontend truncates the indent, the first statement in a line must match the breakpoint (line,0).
</span><span class="cx">         ASSERT(this == m_currentCallFrame-&gt;codeBlock()-&gt;globalObject()-&gt;debugger());
</span><span class="cx">         if ((line != m_lastExecutedLine &amp;&amp; line == breakLine &amp;&amp; !breakColumn)
</span><span class="lines">@@ -454,9 +458,9 @@
</span><span class="cx">         return false;
</span><span class="cx"> 
</span><span class="cx">     if (hitBreakpoint)
</span><del>-        *hitBreakpoint = breakpoints[i];
</del><ins>+        *hitBreakpoint = *breakpoint;
</ins><span class="cx"> 
</span><del>-    if (breakpoints[i].condition.isEmpty())
</del><ins>+    if (breakpoint-&gt;condition.isEmpty())
</ins><span class="cx">         return true;
</span><span class="cx"> 
</span><span class="cx">     // We cannot stop in the debugger while executing condition code,
</span><span class="lines">@@ -465,7 +469,7 @@
</span><span class="cx"> 
</span><span class="cx">     JSValue exception;
</span><span class="cx">     DebuggerCallFrame* debuggerCallFrame = currentDebuggerCallFrame();
</span><del>-    JSValue result = debuggerCallFrame-&gt;evaluate(breakpoints[i].condition, exception);
</del><ins>+    JSValue result = debuggerCallFrame-&gt;evaluate(breakpoint-&gt;condition, exception);
</ins><span class="cx"> 
</span><span class="cx">     // We can lose the debugger while executing JavaScript.
</span><span class="cx">     if (!m_currentCallFrame)
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoredebuggerDebuggerh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/debugger/Debugger.h (170676 => 170677)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/debugger/Debugger.h        2014-07-01 23:40:06 UTC (rev 170676)
+++ trunk/Source/JavaScriptCore/debugger/Debugger.h        2014-07-01 23:40:32 UTC (rev 170677)
</span><span class="lines">@@ -29,7 +29,6 @@
</span><span class="cx"> #include &lt;wtf/HashMap.h&gt;
</span><span class="cx"> #include &lt;wtf/HashSet.h&gt;
</span><span class="cx"> #include &lt;wtf/RefPtr.h&gt;
</span><del>-#include &lt;wtf/Vector.h&gt;
</del><span class="cx"> #include &lt;wtf/text/TextPosition.h&gt;
</span><span class="cx"> 
</span><span class="cx"> namespace JSC {
</span><span class="lines">@@ -128,8 +127,7 @@
</span><span class="cx"> private:
</span><span class="cx">     typedef HashMap&lt;BreakpointID, Breakpoint*&gt; BreakpointIDToBreakpointMap;
</span><span class="cx"> 
</span><del>-    typedef Vector&lt;Breakpoint&gt; BreakpointsInLine;
-    typedef HashMap&lt;unsigned, BreakpointsInLine, WTF::IntHash&lt;int&gt;, WTF::UnsignedWithZeroKeyHashTraits&lt;int&gt;&gt; LineToBreakpointsMap;
</del><ins>+    typedef HashMap&lt;unsigned, RefPtr&lt;BreakpointsList&gt;, WTF::IntHash&lt;int&gt;, WTF::UnsignedWithZeroKeyHashTraits&lt;int&gt;&gt; LineToBreakpointsMap;
</ins><span class="cx">     typedef HashMap&lt;SourceID, LineToBreakpointsMap, WTF::IntHash&lt;SourceID&gt;, WTF::UnsignedWithZeroKeyHashTraits&lt;SourceID&gt;&gt; SourceIDToBreakpointsMap;
</span><span class="cx"> 
</span><span class="cx">     class ClearCodeBlockDebuggerRequestsFunctor;
</span></span></pre>
</div>
</div>

</body>
</html>