<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>161534</bug_id>
          
          <creation_ts>2016-09-02 09:46:04 -0700</creation_ts>
          <short_desc>Regression(r197648): JSObject::setPrototypeWithCycleCheck() allows for cycles but the rest of the code base does not</short_desc>
          <delta_ts>2016-09-08 13:11:00 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>JavaScriptCore</component>
          <version>WebKit Nightly Build</version>
          <rep_platform>Unspecified</rep_platform>
          <op_sys>Unspecified</op_sys>
          <bug_status>NEW</bug_status>
          <resolution></resolution>
          
          <see_also>https://bugs.webkit.org/show_bug.cgi?id=161455</see_also>
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          <blocked>155002</blocked>
          <everconfirmed>1</everconfirmed>
          <reporter name="Chris Dumez">cdumez</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>ggaren</cc>
    
    <cc>mark.lam</cc>
    
    <cc>msaboff</cc>
    
    <cc>saam</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1225791</commentid>
    <comment_count>0</comment_count>
    <who name="Chris Dumez">cdumez</who>
    <bug_when>2016-09-02 09:46:04 -0700</bug_when>
    <thetext>JSObject::setPrototypeWithCycleCheck() allows for cycles but the rest of the code base does not deal properly with cycles.

This is because of the following check that was added:
if (UNLIKELY(asObject(nextPrototype)-&gt;methodTable(vm)-&gt;getPrototype != defaultGetPrototype)) 
  break;

This was added to match the EcmaScript spec:
- https://tc39.github.io/ecma262/#sec-ordinarysetprototypeof (step 8)

However, if you create a cycles, we end up with an infinite loop later on under:
Structure::anyObjectInChainMayInterceptIndexedAccesses()

This is likely not the only place we traverse the prototype chain and except there is no cycle.

I noticed this when running html/browsers/history/the-location-interface/allow_prototype_cycle_through_location.sub.html with the patch for Bug 161455 applied.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1225885</commentid>
    <comment_count>1</comment_count>
    <who name="Saam Barati">saam</who>
    <bug_when>2016-09-02 12:25:05 -0700</bug_when>
    <thetext>(In reply to comment #0)
&gt; JSObject::setPrototypeWithCycleCheck() allows for cycles but the rest of the
&gt; code base does not deal properly with cycles.
&gt; 
&gt; This is because of the following check that was added:
&gt; if (UNLIKELY(asObject(nextPrototype)-&gt;methodTable(vm)-&gt;getPrototype !=
&gt; defaultGetPrototype)) 
&gt;   break;
&gt; 
&gt; This was added to match the EcmaScript spec:
&gt; - https://tc39.github.io/ecma262/#sec-ordinarysetprototypeof (step 8)
&gt; 
&gt; However, if you create a cycles, we end up with an infinite loop later on
&gt; under:
&gt; Structure::anyObjectInChainMayInterceptIndexedAccesses()
&gt; 
&gt; This is likely not the only place we traverse the prototype chain and except
&gt; there is no cycle.
&gt; 
&gt; I noticed this when running
&gt; html/browsers/history/the-location-interface/
&gt; allow_prototype_cycle_through_location.sub.html with the patch for Bug
&gt; 161455 applied.

Yeah, we should teach the rest of the engine that it&apos;s OK to have cycles
in the &quot;getPrototypeDirect&quot; chain.
Currently, inside JSC itself, this will never happen. However, I guess we need to
teach it this w.r.t the HTML spec.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1225908</commentid>
    <comment_count>2</comment_count>
    <who name="Geoffrey Garen">ggaren</who>
    <bug_when>2016-09-02 13:01:39 -0700</bug_when>
    <thetext>&gt; Yeah, we should teach the rest of the engine that it&apos;s OK to have cycles
&gt; in the &quot;getPrototypeDirect&quot; chain.
&gt; Currently, inside JSC itself, this will never happen. However, I guess we
&gt; need to
&gt; teach it this w.r.t the HTML spec.

That&apos;s a pretty big change. Are we sure we need to allow cycles? What&apos;s the expected behavior if you find a cycle? Is it specified?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1226000</commentid>
    <comment_count>3</comment_count>
    <who name="Chris Dumez">cdumez</who>
    <bug_when>2016-09-02 15:45:44 -0700</bug_when>
    <thetext>(In reply to comment #2)
&gt; &gt; Yeah, we should teach the rest of the engine that it&apos;s OK to have cycles
&gt; &gt; in the &quot;getPrototypeDirect&quot; chain.
&gt; &gt; Currently, inside JSC itself, this will never happen. However, I guess we
&gt; &gt; need to
&gt; &gt; teach it this w.r.t the HTML spec.
&gt; 
&gt; That&apos;s a pretty big change. Are we sure we need to allow cycles? What&apos;s the
&gt; expected behavior if you find a cycle? Is it specified?

Note that even without my patch, it is fairly trivial to end up with an infinite loop since r197648 allows for cycles:

&lt;script&gt;
var target = {
  __proto__: null
};
var proxy = new Proxy(target, {});
Object.prototype.__proto__ = {
   __proto__: proxy,
};

var a = {};
a.test;
&lt;/script&gt;

This seems to infinite loop for me on ToT, no DOM involved.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1226003</commentid>
    <comment_count>4</comment_count>
    <who name="Saam Barati">saam</who>
    <bug_when>2016-09-02 15:52:42 -0700</bug_when>
    <thetext>(In reply to comment #3)
&gt; (In reply to comment #2)
&gt; &gt; &gt; Yeah, we should teach the rest of the engine that it&apos;s OK to have cycles
&gt; &gt; &gt; in the &quot;getPrototypeDirect&quot; chain.
&gt; &gt; &gt; Currently, inside JSC itself, this will never happen. However, I guess we
&gt; &gt; &gt; need to
&gt; &gt; &gt; teach it this w.r.t the HTML spec.
&gt; &gt; 
&gt; &gt; That&apos;s a pretty big change. Are we sure we need to allow cycles? What&apos;s the
&gt; &gt; expected behavior if you find a cycle? Is it specified?
&gt; 
&gt; Note that even without my patch, it is fairly trivial to end up with an
&gt; infinite loop since r197648 allows for cycles:
&gt; 
&gt; &lt;script&gt;
&gt; var target = {
&gt;   __proto__: null
&gt; };
&gt; var proxy = new Proxy(target, {});
&gt; Object.prototype.__proto__ = {
&gt;    __proto__: proxy,
&gt; };
&gt; 
&gt; var a = {};
&gt; a.test;
&gt; &lt;/script&gt;
&gt; 
&gt; This seems to infinite loop for me on ToT, no DOM involved.

I believe the bug here is having ProxyObject&apos;s structure&apos;s prototype field
be Object.prototype. That&apos;s wrong.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1226006</commentid>
    <comment_count>5</comment_count>
    <who name="Chris Dumez">cdumez</who>
    <bug_when>2016-09-02 15:55:24 -0700</bug_when>
    <thetext>(In reply to comment #4)
&gt; (In reply to comment #3)
&gt; &gt; (In reply to comment #2)
&gt; &gt; &gt; &gt; Yeah, we should teach the rest of the engine that it&apos;s OK to have cycles
&gt; &gt; &gt; &gt; in the &quot;getPrototypeDirect&quot; chain.
&gt; &gt; &gt; &gt; Currently, inside JSC itself, this will never happen. However, I guess we
&gt; &gt; &gt; &gt; need to
&gt; &gt; &gt; &gt; teach it this w.r.t the HTML spec.
&gt; &gt; &gt; 
&gt; &gt; &gt; That&apos;s a pretty big change. Are we sure we need to allow cycles? What&apos;s the
&gt; &gt; &gt; expected behavior if you find a cycle? Is it specified?
&gt; &gt; 
&gt; &gt; Note that even without my patch, it is fairly trivial to end up with an
&gt; &gt; infinite loop since r197648 allows for cycles:
&gt; &gt; 
&gt; &gt; &lt;script&gt;
&gt; &gt; var target = {
&gt; &gt;   __proto__: null
&gt; &gt; };
&gt; &gt; var proxy = new Proxy(target, {});
&gt; &gt; Object.prototype.__proto__ = {
&gt; &gt;    __proto__: proxy,
&gt; &gt; };
&gt; &gt; 
&gt; &gt; var a = {};
&gt; &gt; a.test;
&gt; &gt; &lt;/script&gt;
&gt; &gt; 
&gt; &gt; This seems to infinite loop for me on ToT, no DOM involved.
&gt; 
&gt; I believe the bug here is having ProxyObject&apos;s structure&apos;s prototype field
&gt; be Object.prototype. That&apos;s wrong.

FYI, I tried fixing proxy.__proto__ to be null like so:
http://pastebin.com/69HRjvPb

But this causes some JSC test failures though:
Tools/Scripts/run-jsc JSTests/stress/proxy-set-prototype-of.js
Running 1 time(s): DYLD_FRAMEWORK_PATH=/Volumes/Data/WebKit/OpenSource/WebKitBuild/Release /Volumes/Data/WebKit/OpenSource/WebKitBuild/Release/jsc JSTests/stress/proxy-set-prototype-of.js
Exception: Error: Bad assertion!
assert@JSTests/stress/proxy-set-prototype-of.js:3:24
global code@JSTests/stress/proxy-set-prototype-of.js:31:19</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1227726</commentid>
    <comment_count>6</comment_count>
    <who name="Chris Dumez">cdumez</who>
    <bug_when>2016-09-08 13:11:00 -0700</bug_when>
    <thetext>EcmaScript bug:
https://github.com/tc39/ecma262/issues/683</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>