<!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>[184960] 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/184960">184960</a></dd>
<dt>Author</dt> <dd>benjamin@webkit.org</dd>
<dt>Date</dt> <dd>2015-05-28 13:57:48 -0700 (Thu, 28 May 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>[iOS8][ARMv7(s)] Optimized Object.create in 'use strict' context sometimes breaks.
https://bugs.webkit.org/show_bug.cgi?id=138038

Reviewed by Michael Saboff.

TL;DR: sometimes the baseline JIT could accidentally nuke the tag before calling
       to C++, making put_by_id behave erratically.

The bug was that put_by_id would randomly not work correctly in 32bits. It happened
in the baseline JIT if we were unlucky enough:
-The code get hot enough and the structure is stable so we get a fast path for
 put_by_id.
-We repatch the fast-path branch with a stub generated by
 emitPutTransitionStubAndGetOldStructure().
-In emitPutTransitionStubAndGetOldStructure(), we only preserve the payload of the base
 register, the tag register is ignored.
-emitPutTransitionStubAndGetOldStructure() allocate 2 to 3 registers. Any of those
 could be the one used for the base's tag before the fast path and the value is trashed.
-If we hit one of the failure case, we fallback to the slow path, but we destroyed
 the tag pointer.
-We now have unrelated bits in the tag, the most likely value type is now &quot;double&quot;
 and we fail the put_by_id because we try to set a property on a number.

The most obvious solution would be to change emitPutTransitionStubAndGetOldStructure()
to preserve the tag register in addition to the value register.
I decided against that option because of the added complexity. The DFG does not need
that case, so I would have to add branches everywhere to distinguish the cases
were we need to preserve the tag or not.

Instead, I just load the tag back from memory in the slow path. The function in the slow
path is several order of magnitude slower than a load, it is not worth eliminating it,
especially in baseline JIT.

I also discovered 4 useless loads in the fast path, so even with my extra load, this patch
makes the baseline faster :)

* jit/JITPropertyAccess32_64.cpp:
(JSC::JIT::emitSlow_op_put_by_id):
(JSC::JIT::emit_op_put_by_id): Deleted.
* tests/stress/put-by-id-on-new-object-after-prototype-transition-non-strict.js: Added.
(opaqueNewObject):
(putValueOnNewObject):
* tests/stress/put-by-id-on-new-object-after-prototype-transition-strict.js: Added.
(string_appeared_here.opaqueNewObject):
(putValueOnNewObject):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCorejitJITPropertyAccess32_64cpp">trunk/Source/JavaScriptCore/jit/JITPropertyAccess32_64.cpp</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoretestsstressputbyidonnewobjectafterprototypetransitionnonstrictjs">trunk/Source/JavaScriptCore/tests/stress/put-by-id-on-new-object-after-prototype-transition-non-strict.js</a></li>
<li><a href="#trunkSourceJavaScriptCoretestsstressputbyidonnewobjectafterprototypetransitionstrictjs">trunk/Source/JavaScriptCore/tests/stress/put-by-id-on-new-object-after-prototype-transition-strict.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (184959 => 184960)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2015-05-28 20:46:14 UTC (rev 184959)
+++ trunk/Source/JavaScriptCore/ChangeLog        2015-05-28 20:57:48 UTC (rev 184960)
</span><span class="lines">@@ -1,5 +1,53 @@
</span><span class="cx"> 2015-05-28  Benjamin Poulain  &lt;benjamin@webkit.org&gt;
</span><span class="cx"> 
</span><ins>+        [iOS8][ARMv7(s)] Optimized Object.create in 'use strict' context sometimes breaks.
+        https://bugs.webkit.org/show_bug.cgi?id=138038
+
+        Reviewed by Michael Saboff.
+
+        TL;DR: sometimes the baseline JIT could accidentally nuke the tag before calling
+               to C++, making put_by_id behave erratically.
+
+        The bug was that put_by_id would randomly not work correctly in 32bits. It happened
+        in the baseline JIT if we were unlucky enough:
+        -The code get hot enough and the structure is stable so we get a fast path for
+         put_by_id.
+        -We repatch the fast-path branch with a stub generated by
+         emitPutTransitionStubAndGetOldStructure().
+        -In emitPutTransitionStubAndGetOldStructure(), we only preserve the payload of the base
+         register, the tag register is ignored.
+        -emitPutTransitionStubAndGetOldStructure() allocate 2 to 3 registers. Any of those
+         could be the one used for the base's tag before the fast path and the value is trashed.
+        -If we hit one of the failure case, we fallback to the slow path, but we destroyed
+         the tag pointer.
+        -We now have unrelated bits in the tag, the most likely value type is now &quot;double&quot;
+         and we fail the put_by_id because we try to set a property on a number.
+
+        The most obvious solution would be to change emitPutTransitionStubAndGetOldStructure()
+        to preserve the tag register in addition to the value register.
+        I decided against that option because of the added complexity. The DFG does not need
+        that case, so I would have to add branches everywhere to distinguish the cases
+        were we need to preserve the tag or not.
+
+        Instead, I just load the tag back from memory in the slow path. The function in the slow
+        path is several order of magnitude slower than a load, it is not worth eliminating it,
+        especially in baseline JIT.
+
+        I also discovered 4 useless loads in the fast path, so even with my extra load, this patch
+        makes the baseline faster :)
+
+        * jit/JITPropertyAccess32_64.cpp:
+        (JSC::JIT::emitSlow_op_put_by_id):
+        (JSC::JIT::emit_op_put_by_id): Deleted.
+        * tests/stress/put-by-id-on-new-object-after-prototype-transition-non-strict.js: Added.
+        (opaqueNewObject):
+        (putValueOnNewObject):
+        * tests/stress/put-by-id-on-new-object-after-prototype-transition-strict.js: Added.
+        (string_appeared_here.opaqueNewObject):
+        (putValueOnNewObject):
+
+2015-05-28  Benjamin Poulain  &lt;benjamin@webkit.org&gt;
+
</ins><span class="cx">         [JSC] reduction the iteration count of the DoubleRep stress tests
</span><span class="cx"> 
</span><span class="cx">         Once again, I used big numbers for manual testing and I forgot to fix them before landing.
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorejitJITPropertyAccess32_64cpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/jit/JITPropertyAccess32_64.cpp (184959 => 184960)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/jit/JITPropertyAccess32_64.cpp        2015-05-28 20:46:14 UTC (rev 184959)
+++ trunk/Source/JavaScriptCore/jit/JITPropertyAccess32_64.cpp        2015-05-28 20:57:48 UTC (rev 184960)
</span><span class="lines">@@ -535,9 +535,6 @@
</span><span class="cx">     emitLoad2(base, regT1, regT0, value, regT3, regT2);
</span><span class="cx">     
</span><span class="cx">     emitJumpSlowCaseIfNotJSCell(base, regT1);
</span><del>-    
-    emitLoad(base, regT1, regT0);
-    emitLoad(value, regT3, regT2);
</del><span class="cx"> 
</span><span class="cx">     JITPutByIdGenerator gen(
</span><span class="cx">         m_codeBlock, CodeOrigin(m_bytecodeOffset), RegisterSet::specialRegisters(),
</span><span class="lines">@@ -559,7 +556,10 @@
</span><span class="cx">     linkSlowCase(iter);
</span><span class="cx">     
</span><span class="cx">     Label coldPathBegin(this);
</span><del>-    
</del><ins>+
+    // JITPutByIdGenerator only preserve the value and the base's payload, we have to reload the tag.
+    emitLoadTag(base, regT1);
+
</ins><span class="cx">     JITPutByIdGenerator&amp; gen = m_putByIds[m_putByIdIndex++];
</span><span class="cx">     
</span><span class="cx">     Call call = callOperation(
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoretestsstressputbyidonnewobjectafterprototypetransitionnonstrictjs"></a>
<div class="addfile"><h4>Added: trunk/Source/JavaScriptCore/tests/stress/put-by-id-on-new-object-after-prototype-transition-non-strict.js (0 => 184960)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/tests/stress/put-by-id-on-new-object-after-prototype-transition-non-strict.js                                (rev 0)
+++ trunk/Source/JavaScriptCore/tests/stress/put-by-id-on-new-object-after-prototype-transition-non-strict.js        2015-05-28 20:57:48 UTC (rev 184960)
</span><span class="lines">@@ -0,0 +1,31 @@
</span><ins>+function opaqueNewObject(prototype)
+{
+    return Object.create(prototype);
+}
+noInline(opaqueNewObject);
+
+function putValueOnNewObject(value, prototype)
+{
+    var object = opaqueNewObject(prototype);
+    object.myProperty = value;
+    return object;
+}
+noInline(putValueOnNewObject)
+
+for (var i = 0; i &lt; 1e4; ++i) {
+    var initialPrototype = new Object;
+    for (var j = 0; j &lt; 5; ++j) {
+        var object = putValueOnNewObject(j, initialPrototype);
+        if (object[&quot;myProperty&quot;] !== j) {
+            throw &quot;Ooops, we mess up before the prototype change at iteration i = &quot; + i + &quot; j = &quot; + j;
+        }
+    }
+
+    initialPrototype.foo = &quot;bar&quot;;
+    for (var j = 0; j &lt; 5; ++j) {
+        var object = putValueOnNewObject(j, initialPrototype);
+        if (object[&quot;myProperty&quot;] !== j) {
+            throw &quot;Ooops, we mess up at iteration i = &quot; + i + &quot; j = &quot; + j;
+        }
+    }
+}
</ins><span class="cx">\ No newline at end of file
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoretestsstressputbyidonnewobjectafterprototypetransitionstrictjs"></a>
<div class="addfile"><h4>Added: trunk/Source/JavaScriptCore/tests/stress/put-by-id-on-new-object-after-prototype-transition-strict.js (0 => 184960)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/tests/stress/put-by-id-on-new-object-after-prototype-transition-strict.js                                (rev 0)
+++ trunk/Source/JavaScriptCore/tests/stress/put-by-id-on-new-object-after-prototype-transition-strict.js        2015-05-28 20:57:48 UTC (rev 184960)
</span><span class="lines">@@ -0,0 +1,33 @@
</span><ins>+&quot;use strict&quot;
+
+function opaqueNewObject(prototype)
+{
+    return Object.create(prototype);
+}
+noInline(opaqueNewObject);
+
+function putValueOnNewObject(value, prototype)
+{
+    var object = opaqueNewObject(prototype);
+    object.myProperty = value;
+    return object;
+}
+noInline(putValueOnNewObject)
+
+for (var i = 0; i &lt; 1e4; ++i) {
+    var initialPrototype = new Object;
+    for (var j = 0; j &lt; 5; ++j) {
+        var object = putValueOnNewObject(j, initialPrototype);
+        if (object[&quot;myProperty&quot;] !== j) {
+            throw &quot;Ooops, we mess up before the prototype change at iteration i = &quot; + i + &quot; j = &quot; + j;
+        }
+    }
+
+    initialPrototype.foo = &quot;bar&quot;;
+    for (var j = 0; j &lt; 5; ++j) {
+        var object = putValueOnNewObject(j, initialPrototype);
+        if (object[&quot;myProperty&quot;] !== j) {
+            throw &quot;Ooops, we mess up at iteration i = &quot; + i + &quot; j = &quot; + j;
+        }
+    }
+}
</ins><span class="cx">\ No newline at end of file
</span></span></pre>
</div>
</div>

</body>
</html>