<?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>97767</bug_id>
          
          <creation_ts>2012-09-27 02:20:56 -0700</creation_ts>
          <short_desc>[V8] StringCache::v8ExternalString() can return a stale persistent handle</short_desc>
          <delta_ts>2012-09-27 21:27:30 -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>WebCore JavaScript</component>
          <version>528+ (Nightly build)</version>
          <rep_platform>Unspecified</rep_platform>
          <op_sys>Unspecified</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Kentaro Hara">haraken</reporter>
          <assigned_to name="Kentaro Hara">haraken</assigned_to>
          <cc>abarth</cc>
    
    <cc>dcarney</cc>
    
    <cc>japhet</cc>
    
    <cc>webkit.review.bot</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>729338</commentid>
    <comment_count>0</comment_count>
    <who name="Kentaro Hara">haraken</who>
    <bug_when>2012-09-27 02:20:56 -0700</bug_when>
    <thetext>For details, see the Chromium bug: http://code.google.com/p/chromium/issues/detail?id=151902

StringCache::v8ExternalString() can return a stale persistent handle in the following scenario:

(1) Assume that StringImpl A with value &quot;foo&quot; is in m_stringCache.
(2) StringImpl B with value &quot;foo&quot; is accessed. At this point, m_lastStringImpl points to B, and m_lastV8String points to B&apos;s handle.
(3) A minor GC is triggered and a weak callback is called back for StringImpl A. At this point, &quot;foo&quot; is removed from m_stringCache. A&apos;s handle is disposed. However, m_lastV8String is not cleared because m_lastStringImpl (i.e. StringImpl B) is not equal to StringImpl A. As a result, m_lastV8String points to a stale persistent handle.
(4) The persistent handle is eventually reused in V8 and made weak again.
(5) StringImpl B with value &quot;foo&quot; is accessed. Then StringCache::v8ExternalString() returns the stale persistent handle, which is already used for another purpose.

To solve the problem, we need to clear m_stringImpl and m_lastV8String when any string wrapper is disposed. Specifically, we need to change the code like this:

  static void cachedStringCallback(v8::Persistent&lt;v8::Value&gt; wrapper, void* parameter)
  {
    StringImpl* stringImpl = static_cast&lt;StringImpl*&gt;(parameter);
    V8PerIsolateData::current()-&gt;stringCache()-&gt;remove(stringImpl);
    wrapper.Dispose();
    stringImpl-&gt;deref();
  }

  void StringCache::remove(StringImpl* stringImpl)
  {
    m_stringCache.remove(stringImpl);
    if (m_lastStringImpl.get() == stringImpl) {  // Remove this line.
        m_lastStringImpl = 0;
        m_lastV8String.Clear();
    }
  }

Note: Removing the line might be stronger than is needed. Instead of removing the line, we can just replace the line with &apos;if (m_lastV8String == wrapper)&apos;. However, just in case (for correctness), I&apos;d prefer removing the line. Given that GC won&apos;t happen so frequently, clearing the cache in every weak callback won&apos;t affect performance.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>729341</commentid>
    <comment_count>1</comment_count>
      <attachid>165956</attachid>
    <who name="Kentaro Hara">haraken</who>
    <bug_when>2012-09-27 02:23:29 -0700</bug_when>
    <thetext>Created attachment 165956
Patch</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>729633</commentid>
    <comment_count>2</comment_count>
      <attachid>165956</attachid>
    <who name="Adam Barth">abarth</who>
    <bug_when>2012-09-27 09:49:40 -0700</bug_when>
    <thetext>Comment on attachment 165956
Patch

I&apos;m marking this patch r+, but I asked haraken about whether some information in crbug.com/151902 would make it possible to write a test for this patch.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>729668</commentid>
    <comment_count>3</comment_count>
    <who name="Adam Barth">abarth</who>
    <bug_when>2012-09-27 10:22:55 -0700</bug_when>
    <thetext>Apparently the example we have is really fragile and depends on details of the allocator and GC to trigger.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>729895</commentid>
    <comment_count>4</comment_count>
      <attachid>165956</attachid>
    <who name="Kentaro Hara">haraken</who>
    <bug_when>2012-09-27 15:00:01 -0700</bug_when>
    <thetext>Comment on attachment 165956
Patch

&gt; Apparently the example we have is really fragile and depends on details of the allocator and GC to trigger.

Yeah, it depends on the behavior of a minor GC, which is difficult to test.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>729905</commentid>
    <comment_count>5</comment_count>
      <attachid>165956</attachid>
    <who name="WebKit Review Bot">webkit.review.bot</who>
    <bug_when>2012-09-27 15:04:51 -0700</bug_when>
    <thetext>Comment on attachment 165956
Patch

Rejecting attachment 165956 from commit-queue.

Failed to run &quot;[&apos;/mnt/git/webkit-commit-queue/Tools/Scripts/webkit-patch&apos;, &apos;--status-host=queues.webkit.org&apos;, &apos;-...&quot; exit_code: 1

ERROR: /mnt/git/webkit-commit-queue/Source/WebKit/mac/ChangeLog neither lists a valid reviewer nor contains the string &quot;Unreviewed&quot; or &quot;Rubber stamp&quot; (case insensitive).

Full output: http://queues.webkit.org/results/14054068</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>729981</commentid>
    <comment_count>6</comment_count>
      <attachid>165956</attachid>
    <who name="WebKit Review Bot">webkit.review.bot</who>
    <bug_when>2012-09-27 16:25:21 -0700</bug_when>
    <thetext>Comment on attachment 165956
Patch

Rejecting attachment 165956 from commit-queue.

Failed to run &quot;[&apos;/mnt/git/webkit-commit-queue/Tools/Scripts/webkit-patch&apos;, &apos;--status-host=queues.webkit.org&apos;, &apos;-...&quot; exit_code: 2

Last 500 characters of output:

CONFLICT (content): Merge conflict in Source/WebCore/ChangeLog
Failed to merge in the changes.
Patch failed at 0001 Results page should warn about time-dependent distributions

When you have resolved this problem run &quot;git rebase --continue&quot;.
If you would prefer to skip this patch, instead run &quot;git rebase --skip&quot;.
To restore the original branch and stop rebasing run &quot;git rebase --abort&quot;.

rebase refs/remotes/origin/master: command returned error: 1

Died at Tools/Scripts/update-webkit line 164.

Full output: http://queues.webkit.org/results/14038844</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>730013</commentid>
    <comment_count>7</comment_count>
      <attachid>165956</attachid>
    <who name="WebKit Review Bot">webkit.review.bot</who>
    <bug_when>2012-09-27 16:52:37 -0700</bug_when>
    <thetext>Comment on attachment 165956
Patch

Rejecting attachment 165956 from commit-queue.

Failed to run &quot;[&apos;/mnt/git/webkit-commit-queue/Tools/Scripts/webkit-patch&apos;, &apos;--status-host=queues.webkit.org&apos;, &apos;-...&quot; exit_code: 1

Last 500 characters of output:
Kit/chromium/third_party/yasm/source/patched-yasm --revision 154708 --non-interactive --force --accept theirs-conflict --ignore-externals&apos; in &apos;/mnt/git/webkit-commit-queue/Source/WebKit/chromium&apos;
51&gt;At revision 154708.

________ running &apos;/usr/bin/python tools/clang/scripts/update.py --mac-only&apos; in &apos;/mnt/git/webkit-commit-queue/Source/WebKit/chromium&apos;

________ running &apos;/usr/bin/python gyp_webkit&apos; in &apos;/mnt/git/webkit-commit-queue/Source/WebKit/chromium&apos;
Updating webkit projects from gyp files...

Full output: http://queues.webkit.org/results/14036965</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>730015</commentid>
    <comment_count>8</comment_count>
    <who name="Kentaro Hara">haraken</who>
    <bug_when>2012-09-27 16:53:09 -0700</bug_when>
    <thetext>hmm, let me try to land it later.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>730222</commentid>
    <comment_count>9</comment_count>
      <attachid>165956</attachid>
    <who name="WebKit Review Bot">webkit.review.bot</who>
    <bug_when>2012-09-27 21:27:27 -0700</bug_when>
    <thetext>Comment on attachment 165956
Patch

Clearing flags on attachment: 165956

Committed r129849: &lt;http://trac.webkit.org/changeset/129849&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>730223</commentid>
    <comment_count>10</comment_count>
    <who name="WebKit Review Bot">webkit.review.bot</who>
    <bug_when>2012-09-27 21:27:30 -0700</bug_when>
    <thetext>All reviewed patches have been landed.  Closing bug.</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="1"
              isprivate="0"
          >
            <attachid>165956</attachid>
            <date>2012-09-27 02:23:29 -0700</date>
            <delta_ts>2012-09-27 21:27:27 -0700</delta_ts>
            <desc>Patch</desc>
            <filename>bug-97767-20120927182247.patch</filename>
            <type>text/plain</type>
            <size>3895</size>
            <attacher name="Kentaro Hara">haraken</attacher>
            
              <data encoding="base64">U3VidmVyc2lvbiBSZXZpc2lvbjogMTI5NzIyCmRpZmYgLS1naXQgYS9Tb3VyY2UvV2ViQ29yZS9D
aGFuZ2VMb2cgYi9Tb3VyY2UvV2ViQ29yZS9DaGFuZ2VMb2cKaW5kZXggYWExZTYxN2UwM2U4MTA2
MGM0YmUzNTZlZDUwYjA5ZjEwNzNmN2JjMS4uMjcxN2ExMWE2ODcwY2JjZjg0ODkzN2I0YmQyNmNm
NmRkMjZmOGYzMCAxMDA2NDQKLS0tIGEvU291cmNlL1dlYkNvcmUvQ2hhbmdlTG9nCisrKyBiL1Nv
dXJjZS9XZWJDb3JlL0NoYW5nZUxvZwpAQCAtMSwzICsxLDU4IEBACisyMDEyLTA5LTI3ICBLZW50
YXJvIEhhcmEgIDxoYXJha2VuQGNocm9taXVtLm9yZz4KKworICAgICAgICBbVjhdIFN0cmluZ0Nh
Y2hlOjp2OEV4dGVybmFsU3RyaW5nKCkgY2FuIHJldHVybiBhIHN0YWxlIHBlcnNpc3RlbnQgaGFu
ZGxlCisgICAgICAgIGh0dHBzOi8vYnVncy53ZWJraXQub3JnL3Nob3dfYnVnLmNnaT9pZD05Nzc2
NworCisgICAgICAgIFJldmlld2VkIGJ5IE5PQk9EWSAoT09QUyEpLgorCisgICAgICAgIEZvciBk
ZXRhaWxzLC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