<?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>131682</bug_id>
          
          <creation_ts>2014-04-15 11:23:41 -0700</creation_ts>
          <short_desc>Reproducible crash in JavaScriptCore: JSObjectMakeArray()</short_desc>
          <delta_ts>2015-03-30 13:21:28 -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>528+ (Nightly build)</version>
          <rep_platform>Mac (Intel)</rep_platform>
          <op_sys>OS X 10.9</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>INVALID</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P1</priority>
          <bug_severity>Critical</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Alexander Meißner">AlexanderMeissner</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>adam.fedor</cc>
    
    <cc>ggaren</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1000899</commentid>
    <comment_count>0</comment_count>
    <who name="Alexander Meißner">AlexanderMeissner</who>
    <bug_when>2014-04-15 11:23:41 -0700</bug_when>
    <thetext>Reproducible crash in JavaScriptCore:
JS_EXPORT JSObjectRef JSObjectMakeArray(JSContextRef ctx, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) CF_AVAILABLE(10_6, 7_0);

The attached source code crashes every time the same way.
No JS exception is thrown and there are serval ways to manipulate when it crashes, but it always crashes in JSObjectMakeArray().
I&apos;m not sure if I&apos;m doing something fundamentally wrong or if it really is a bug in JavaScriptCore its self.
The circumstances which are needed to reproduce the crash are very odd and I randomly came across this bug by implementing a linear algebra JS interface for a separate project.


Call stack:
#0	0x00000001018d7057 in JSC::StructureIDTable::get(unsigned int) [inlined] at WebKit/Source/JavaScriptCore/runtime/StructureIDTable.h:86
#1	0x00000001018d7050 in JSC::JSCell::structure(JSC::VM&amp;) const [inlined] at WebKit/Source/JavaScriptCore/runtime/JSCellInlines.h:104
#2	0x00000001018d7050 in JSC::JSCell::methodTable() const [inlined] at WebKit/Source/JavaScriptCore/runtime/JSCellInlines.h:191
#3	0x00000001018d7036 in toJS(JSC::ExecState*, OpaqueJSValue const*) [inlined] at WebKit/Source/JavaScriptCore/API/APICast.h:81
#4	0x00000001018d702e in JSObjectMakeArray at WebKit/Source/JavaScriptCore/API/JSObjectRef.cpp:178


Tested r167020: One built by my self and also the JavaScriptCore.framework of WebKit nightly
MacBook Pro Retina, Mid 2012, 64-Bit, MacOS 10.8.5 and another one (identical hardware) but with MacOS 10.9


All of the following ways stop the test from crashing or change the iteration it crashes:

1. context = JSGlobalContextCreate(NULL); //instead of JSClassCreate(&amp;kJSClassDefinitionEmpty)

2. not adding the property &quot;test&quot; to the contexts global object

3. using stack memory instead of heap for the valueArray passed to JSObjectMakeArray() like:
JSValueRef valueArray[4];
or
JSValueRef* valueArray = (JSValueRef*)alloca(sizeof(JSValueRef)*4);

4. adding another than 3 or 4 properties to the vector instances
0 properties: All Iterations
1 property: All Iterations
2 properties: All Iterations
3 properties: 326 Iterations
4 properties: 285 Iterations
5 properties: All Iterations
6 properties: All Iterations

5. Not passing a JSClassRef to:
JSObjectRef instance = JSObjectMake(context, NULL, NULL);

6. Creating less than 4 instances of the vector class in one iteration:
0 to 3 instances per iteration: All Iterations
4 instances per iteration: 326 Iterations
5 instances per iteration: 575 Iterations
6 instances per iteration: 217 Iterations
7 instances per iteration: 186 Iterations
8 instances per iteration: 163 Iterations

7. Adjusting the argumentCount parameter of JSObjectMakeArray() to the actual count of instances per iteration changes the behavior too

But not a single one of them is a work around as I need the complete functionality of JavaScriptCore.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1000900</commentid>
    <comment_count>1</comment_count>
      <attachid>229382</attachid>
    <who name="Alexander Meißner">AlexanderMeissner</who>
    <bug_when>2014-04-15 11:27:56 -0700</bug_when>
    <thetext>Created attachment 229382
Source code to reproduce the crash

When compiling and running the code, be sure to link against the JavaScriptCore.framework of WebKit Nightly not the one of your system

You can check that by using: otool -L [executable]

Which should show something like:
@executable_path/JavaScriptCore.framework/Versions/A/JavaScriptCore
instead of:
/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/JavaScriptCore</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1063915</commentid>
    <comment_count>2</comment_count>
    <who name="Adam">adam.fedor</who>
    <bug_when>2015-01-24 08:44:55 -0800</bug_when>
    <thetext>I reproduced this on my machine (Mid-2011 Mini, OS X 10.10.1) Using Webkit svn rev 176947.  I&apos;m interested in this as it seems very similar to a bug I reported to Apple rdar://19378158 (http://openradar.appspot.com/radar?id=6174800997253120)</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1064061</commentid>
    <comment_count>3</comment_count>
    <who name="Adam">adam.fedor</who>
    <bug_when>2015-01-25 18:23:05 -0800</bug_when>
    <thetext>Actually, now I think it might be just an issue with the example. I can&apos;t claim to know anything about how JavaScript works, but I think the newly created objects aren&apos;t on the stack, so the garbage collector doesn&apos;t know about them, so occasionally an object just gets deallocated. Doing something like this fixes the issue:

JSValueRef a = newVectorInstance()
valueArray[0] = a;
...etc...

Does anyone know if that&apos;s right or if there is another way to keep an object from being collected? (JSValueProtect/JSValueUnprotect also works, but it seems like that is if you want the objects to be global).</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1064139</commentid>
    <comment_count>4</comment_count>
    <who name="Alexander Meißner">AlexanderMeissner</who>
    <bug_when>2015-01-26 05:25:24 -0800</bug_when>
    <thetext>(In reply to comment #3)

This is actually more the way C/C++ works that matters than the way JavaScript works.

The difference is that

JSValueRef a = newVectorInstance();
valueArray[0] = a;

orders the compiler to use the stack, while

valueArray[0] = newVectorInstance();

is a temporary value which could be held in registers.
But this is already known to be part of the bug,
using the stack is some kind of work around:

JSValueRef* valueArray = (JSValueRef*)alloca(sizeof(JSValueRef)*4);


I suspect that the problem might be in the Construction/Destruction of JSValue (as that would be stack related)
or a memory access in the heap that goes wrong, caused by a early delete/free of memory.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1081283</commentid>
    <comment_count>5</comment_count>
    <who name="Geoffrey Garen">ggaren</who>
    <bug_when>2015-03-30 13:21:28 -0700</bug_when>
    <thetext>&gt;     JSValueRef* valueArray = new JSValueRef[8];

It&apos;s not valid to put a JSValueRef in the heap without first calling JSValueProtect.

The garbage collector will automatically scan JSValueRefs on the stack, but once you put the JSValueRef into the heap like this, you need to use explicit reference counting through JSValueProtect and JSValueUnprotect.</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="0"
              isprivate="0"
          >
            <attachid>229382</attachid>
            <date>2014-04-15 11:27:56 -0700</date>
            <delta_ts>2014-04-15 11:29:39 -0700</delta_ts>
            <desc>Source code to reproduce the crash</desc>
            <filename>main.cpp</filename>
            <type>text/plain-text</type>
            <size>3049</size>
            <attacher name="Alexander Meißner">AlexanderMeissner</attacher>
            
              <data encoding="base64">Ly8KLy8gIG1haW4uY3BwCi8vICBXZWJLaXRCdWdUZXN0Ci8vCi8vICBDcmVhdGVkIGJ5IEFsZXhh
bmRlciBNZWnDn25lciBvbiAxNS4wNC4xNC4KLy8gIENvcHlyaWdodCAoYykgMjAxNCBBbGV4YW5k
ZXIgTWVpw59uZXIuIEFsbCByaWdodHMgcmVzZXJ2ZWQuCi8vCgojaW5jbHVkZSA8SmF2YVNjcmlw
dENvcmUvSmF2YVNjcmlwdC5oPgoKSlNHbG9iYWxDb250ZXh0UmVmIGNvbnRleHQ7CkpTQ2xhc3NS
ZWYgdmVjdG9yQ2xhc3M7C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4iLCBpKTsKICAgICAgICBKU09iamVj
dE1ha2VBcnJheShjb250ZXh0LCA0LCB2YWx1ZUFycmF5LCAmZXhjZXB0aW9uKTsKICAgICAgICAK
ICAgICAgICBpZihleGNlcHRpb24pCiAgICAgICAgICAgIHByaW50ZigiRXhjZXB0aW9uICVwXG4i
LCBleGNlcHRpb24pOyAvL05ldmVyIHRocm93bgogICAgfQogICAgCiAgICAvL05ldmVyIHJlYWNo
ZWQKICAgIGRlbGV0ZSBbXSB2YWx1ZUFycmF5OwogICAgSlNHbG9iYWxDb250ZXh0UmVsZWFzZShj
b250ZXh0KTsKICAgIEpTU3RyaW5nUmVsZWFzZShzdHJYKTsKICAgIEpTU3RyaW5nUmVsZWFzZShz
dHJZKTsKICAgIEpTU3RyaW5nUmVsZWFzZShzdHJaKTsKICAgIEpTU3RyaW5nUmVsZWFzZShzdHJU
ZXN0KTsKICAgIAogICAgcmV0dXJuIDA7Cn0KCg==
</data>

          </attachment>
      

    </bug>

</bugzilla>