Segmentation fault (Core dumped)

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

Segmentation fault (Core dumped)

Sara
Hi, All
While I was executing multithreaded cpp application and embed javascript inside, I got this error Segmentation fault (core dumped) and I tried to debug through netbeans but the pointer of execution get stuck at the beginning of thread function and only execute the main function.


Here is the code, it is very simple only one thread which is created by main
what is the problem here?

/**********************************start***********************/
#include "jsapi.h"
#include <pthread.h>
using namespace std;

/* The class of the global object. */
JSRuntime *rt;
JSObject  *global;
static JSClass global_class = {
    "global", JSCLASS_GLOBAL_FLAGS,
    JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
    JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
    JSCLASS_NO_OPTIONAL_MEMBERS
};

/* The error reporter callback. */
void reportError(JSContext *cx, const char *message, JSErrorReport *report)
{
    fprintf(stderr, "%s:%u:%s\n",
            report->filename ? report->filename : "<no filename=\"filename\">",
            (unsigned int) report->lineno,
            message);
}
/** the function which the thread executes when is created at main
 *@parameter structure of all the argument the function needs
 *@return return null at the end
*/
void* threadFunction(void* arg){
 /* indicates that the execution of the script is executed successfully or not*/
 bool execute;
 char threadScript[] = "'Hi ' + 'Sara!'";
 char *scr = threadScript;
  JSString *outStr;
 jsval rval1;
  JSContext *currContext;
currContext = JS_NewContext(rt, 8192);
  if (currContext == NULL)
       return NULL;

  JS_BeginRequest(currContext);
   JS_SetOptions(currContext, JSOPTION_VAROBJFIX | JSOPTION_JIT | JSOPTION_METHODJIT);
   JS_SetVersion(currContext, JSVERSION_LATEST);

  JSObject *compiledScript = JS_CompileScript(currContext, global, scr,strlen(scr), "threadFilename", 0);

  execute = JS_ExecuteScript(currContext, global, compiledScript, &rval1);
      if ( rval1 == JS_FALSE)//rval == JS_NULL |
        return NULL;
    outStr = JS_ValueToString(currContext, rval1);
    printf("%s\n", JS_EncodeString(currContext, outStr));
    JS_EndRequest(currContext);
     JS_DestroyContext(currContext);
 
    return NULL;
   
}
int main(int argc, const char *argv[]) {
    JSContext *cx;  
/* id of the created thread*/
    pthread_t thread_id;
    /* Create a JS runtime. You always need at least one runtime per process. */
    rt = JS_NewRuntime(8 * 1024 * 1024);
    if (rt == NULL)
        return 1;
    cx = JS_NewContext(rt, 8192);
    if (cx == NULL)
        return 1;
    JS_BeginRequest(cx);
    JS_SetOptions(cx, JSOPTION_VAROBJFIX | JSOPTION_JIT | JSOPTION_METHODJIT);
    JS_SetVersion(cx, JSVERSION_LATEST);
    JS_SetErrorReporter(cx, reportError);
    global = JS_NewCompartmentAndGlobalObject(cx, &global_class, NULL);
    if (global == NULL)
        return 1;
    if (!JS_InitStandardClasses(cx, global))
        return 1;
     char threadScript[] = "'Hi ' + 'Sara!'";
    pthread_create(&thread_id, NULL, &threadFunction,NULL);

    const char *script = "'Hello ' + 'World!'";
    jsval rval;
    JSString *str;
    JSBool ok;
    const char *filename = "noname";
    uintN lineno = 0;
    ok = JS_EvaluateScript(cx, global, script, strlen(script),
                           filename, lineno, &rval);
    if ( rval == JS_FALSE)//rval == JS_NULL |
        return 1;
    str = JS_ValueToString(cx, rval);
    printf("%s\n", JS_EncodeString(cx, str));

    /* End of your application code */

    /* Clean things up and shut down SpiderMonkey. */
    JS_EndRequest(cx);
    JS_DestroyContext(cx);
    JS_DestroyRuntime(rt);
    JS_ShutDown();
   
    return 0;
}

/**************************end**************************/
Thank you in advance
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Segmentation fault (Core dumped)

Yves
> Here is the code, it is very simple only one thread which is created by
> main what is the problem here?

Hi,

One thing I notice at a glace is that you are using the same runtime from
multiple threads. You have to use one runtime per thread.

Also I recommend using a debug build of the library. A backtrace often
gives you good hints where the problem is.

Regards,
Yves

_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Segmentation fault (Core dumped)

Anthony Catel-4
In reply to this post by Sara
It's goinf hard to help you if you don't provide a backtrace.
Btw, it looks like you are using the same runtime in your second thread. JSAPI requires you to create a new runtime for each thread

A.

> On 04.01.2014, at 16:00, Sara <[hidden email]> wrote:
>
> Hi, All
> While I was executing multithreaded cpp application and embed javascript inside, I got this error Segmentation fault (core dumped) and I tried to debug through netbeans but the pointer of execution get stuck at the beginning of thread function and only execute the main function.
>
>
> Here is the code, it is very simple only one thread which is created by main
> what is the problem here?
>
> /**********************************start***********************/
> #include "jsapi.h"
> #include <pthread.h>
> using namespace std;
>
> /* The class of the global object. */
> JSRuntime *rt;
> JSObject  *global;
> static JSClass global_class = {
>    "global", JSCLASS_GLOBAL_FLAGS,
>    JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
>    JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
>    JSCLASS_NO_OPTIONAL_MEMBERS
> };
>
> /* The error reporter callback. */
> void reportError(JSContext *cx, const char *message, JSErrorReport *report)
> {
>    fprintf(stderr, "%s:%u:%s\n",
>            report->filename ? report->filename : "<no filename=\"filename\">",
>            (unsigned int) report->lineno,
>            message);
> }
> /** the function which the thread executes when is created at main
> *@parameter structure of all the argument the function needs
> *@return return null at the end
> */
> void* threadFunction(void* arg){
> /* indicates that the execution of the script is executed successfully or not*/
> bool execute;
> char threadScript[] = "'Hi ' + 'Sara!'";
> char *scr = threadScript;
>  JSString *outStr;
> jsval rval1;
>  JSContext *currContext;
> currContext = JS_NewContext(rt, 8192);
>  if (currContext == NULL)
>       return NULL;
>
>  JS_BeginRequest(currContext);
>   JS_SetOptions(currContext, JSOPTION_VAROBJFIX | JSOPTION_JIT | JSOPTION_METHODJIT);
>   JS_SetVersion(currContext, JSVERSION_LATEST);
>
>  JSObject *compiledScript = JS_CompileScript(currContext, global, scr,strlen(scr), "threadFilename", 0);
>
>  execute = JS_ExecuteScript(currContext, global, compiledScript, &rval1);
>      if ( rval1 == JS_FALSE)//rval == JS_NULL |
>        return NULL;
>    outStr = JS_ValueToString(currContext, rval1);
>    printf("%s\n", JS_EncodeString(currContext, outStr));
>    JS_EndRequest(currContext);
>     JS_DestroyContext(currContext);
>
>    return NULL;
>
> }
> int main(int argc, const char *argv[]) {
>    JSContext *cx;  
> /* id of the created thread*/
>    pthread_t thread_id;
>    /* Create a JS runtime. You always need at least one runtime per process. */
>    rt = JS_NewRuntime(8 * 1024 * 1024);
>    if (rt == NULL)
>        return 1;
>    cx = JS_NewContext(rt, 8192);
>    if (cx == NULL)
>        return 1;
>    JS_BeginRequest(cx);
>    JS_SetOptions(cx, JSOPTION_VAROBJFIX | JSOPTION_JIT | JSOPTION_METHODJIT);
>    JS_SetVersion(cx, JSVERSION_LATEST);
>    JS_SetErrorReporter(cx, reportError);
>    global = JS_NewCompartmentAndGlobalObject(cx, &global_class, NULL);
>    if (global == NULL)
>        return 1;
>    if (!JS_InitStandardClasses(cx, global))
>        return 1;
>     char threadScript[] = "'Hi ' + 'Sara!'";
>    pthread_create(&thread_id, NULL, &threadFunction,NULL);
>
>    const char *script = "'Hello ' + 'World!'";
>    jsval rval;
>    JSString *str;
>    JSBool ok;
>    const char *filename = "noname";
>    uintN lineno = 0;
>    ok = JS_EvaluateScript(cx, global, script, strlen(script),
>                           filename, lineno, &rval);
>    if ( rval == JS_FALSE)//rval == JS_NULL |
>        return 1;
>    str = JS_ValueToString(cx, rval);
>    printf("%s\n", JS_EncodeString(cx, str));
>
>    /* End of your application code */
>
>    /* Clean things up and shut down SpiderMonkey. */
>    JS_EndRequest(cx);
>    JS_DestroyContext(cx);
>    JS_DestroyRuntime(rt);
>    JS_ShutDown();
>
>    return 0;
> }
>
> /**************************end**************************/
> Thank you in advance
> _______________________________________________
> dev-tech-js-engine mailing list
> [hidden email]
> https://lists.mozilla.org/listinfo/dev-tech-js-engine
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Segmentation fault (Core dumped)

Sara
In reply to this post by Yves
On Saturday, January 4, 2014 5:17:22 PM UTC+2, Yves wrote:

> > Here is the code, it is very simple only one thread which is created by
>
> > main what is the problem here?
>
>
>
> Hi,
>
>
>
> One thing I notice at a glace is that you are using the same runtime from
>
> multiple threads. You have to use one runtime per thread.
>
>
>
> Also I recommend using a debug build of the library. A backtrace often
>
> gives you good hints where the problem is.
>
>
>
> Regards,
>
> Yves
Thank you, but I have a question regarding one runtime per thread, as I understood from spidermonkey documentation that your application must have only one runtime perthread, even if you have many threads here you can find what I am talking about https://developer.mozilla.org/en-US/docs/SpiderMonkey/Internals/Thread_Safety 
"SpiderMonkey has a top-level struct, JSRuntime, that acts as a container for everything else. A program typically has only one JSRuntime, even if it has many threads." and it was updated since November 2013.

the 2nd question, does I need to enable thread safety option while compiling my application, as I didn't build spidermonkey with enabling thread safety before.

Thank you,
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Segmentation fault (Core dumped)

Anthony Catel-4
As stated on the link you pasted :
Note: Starting in Gecko 12.0, JSRuntime is single-threaded. You must only use it from one thread.



A.

> On 04.01.2014, at 21:43, Sara <[hidden email]> wrote:
>
> On Saturday, January 4, 2014 5:17:22 PM UTC+2, Yves wrote:
>>> Here is the code, it is very simple only one thread which is created by
>>
>>> main what is the problem here?
>>
>>
>>
>> Hi,
>>
>>
>>
>> One thing I notice at a glace is that you are using the same runtime from
>>
>> multiple threads. You have to use one runtime per thread.
>>
>>
>>
>> Also I recommend using a debug build of the library. A backtrace often
>>
>> gives you good hints where the problem is.
>>
>>
>>
>> Regards,
>>
>> Yves
> Thank you, but I have a question regarding one runtime per thread, as I understood from spidermonkey documentation that your application must have only one runtime perthread, even if you have many threads here you can find what I am talking about https://developer.mozilla.org/en-US/docs/SpiderMonkey/Internals/Thread_Safety 
> "SpiderMonkey has a top-level struct, JSRuntime, that acts as a container for everything else. A program typically has only one JSRuntime, even if it has many threads." and it was updated since November 2013.
>
> the 2nd question, does I need to enable thread safety option while compiling my application, as I didn't build spidermonkey with enabling thread safety before.
>
> Thank you,
> _______________________________________________
> dev-tech-js-engine mailing list
> [hidden email]
> https://lists.mozilla.org/listinfo/dev-tech-js-engine
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Segmentation fault (Core dumped)

Sara
In reply to this post by Sara
On Saturday, January 4, 2014 11:41:47 PM UTC+2, Anthony Catel wrote:
> As stated on the link you pasted :
>
> Note: Starting in Gecko 12.0, JSRuntime is single-threaded. You must only use it from one thread.

I tried also, to create new runtime for the single thread that I have in the code but gives me the same error?
I guess that there is something wrong on the command that I write,
 I use this one
"g++ main.cpp -o main -I/usr/local/include/js -I/usr/local/lib -lmozjs185"
should I enable thread safety option? if yes, how can I do this in the command? also should I mention pthread library in linking command also?

Thank you,
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Segmentation fault (Core dumped)

Anthony Catel-4
You need to link against nspr. ./configure --help
Should guve you some hints.

A.

> On 04.01.2014, at 23:09, Sara <[hidden email]> wrote:
>
>> On Saturday, January 4, 2014 11:41:47 PM UTC+2, Anthony Catel wrote:
>> As stated on the link you pasted :
>>
>> Note: Starting in Gecko 12.0, JSRuntime is single-threaded. You must only use it from one thread.
>
> I tried also, to create new runtime for the single thread that I have in the code but gives me the same error?
> I guess that there is something wrong on the command that I write,
> I use this one
> "g++ main.cpp -o main -I/usr/local/include/js -I/usr/local/lib -lmozjs185"
> should I enable thread safety option? if yes, how can I do this in the command? also should I mention pthread library in linking command also?
>
> Thank you,
> _______________________________________________
> dev-tech-js-engine mailing list
> [hidden email]
> https://lists.mozilla.org/listinfo/dev-tech-js-engine
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Segmentation fault (Core dumped)

Sara
In reply to this post by Sara

> I tried also, to create new runtime for the single thread that I have in the code but gives me the same error?
>
> I guess that there is something wrong on the command that I write,
>
>  I use this one
>
> "g++ main.cpp -o main -I/usr/local/include/js -I/usr/local/lib -lmozjs185"
>
> should I enable thread safety option? if yes, how can I do this in the command? also should I mention pthread library in linking command also?
>
>
>
> Thank you,
Also, what about spider monkey version 1.8.5 from runtime perspective ?
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Segmentation fault (Core dumped)

Anthony Catel-4
IIRC 1.8.5 is not jsruntime-thread-safe.

A.

> On 04.01.2014, at 23:19, Sara <[hidden email]> wrote:
>
>
>> I tried also, to create new runtime for the single thread that I have in the code but gives me the same error?
>>
>> I guess that there is something wrong on the command that I write,
>>
>> I use this one
>>
>> "g++ main.cpp -o main -I/usr/local/include/js -I/usr/local/lib -lmozjs185"
>>
>> should I enable thread safety option? if yes, how can I do this in the command? also should I mention pthread library in linking command also?
>>
>>
>>
>> Thank you,
> Also, what about spider monkey version 1.8.5 from runtime perspective ?
> _______________________________________________
> dev-tech-js-engine mailing list
> [hidden email]
> https://lists.mozilla.org/listinfo/dev-tech-js-engine
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Segmentation fault (Core dumped)

Sara
In reply to this post by Sara
On Sunday, January 5, 2014 12:17:04 AM UTC+2, Anthony Catel wrote:
> You need to link against nspr. ./configure --help
>
> Should guve you some hints.
>
 so, I should install and link nspr library, also ./configure file will help me, that's right ?

and really thank you for helping me.

_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Segmentation fault (Core dumped)

Yves
In reply to this post by Sara
> Also, what about spider monkey version 1.8.5 from runtime perspective ?

You shouldn't use SpiderMonkey 1.8.5 for new applications!
It's completely outdated, doesn't get patched anymore, contains a lot of
bugs that are fixed in the meantime and works much different in many
aspects than the newer versions. Upgrading later is a pain!
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Segmentation fault (Core dumped)

Sara
On Sunday, January 5, 2014 1:28:40 AM UTC+2, Yves wrote:

> > Also, what about spider monkey version 1.8.5 from runtime perspective ?
>
>
>
> You shouldn't use SpiderMonkey 1.8.5 for new applications!
>
> It's completely outdated, doesn't get patched anymore, contains a lot of
>
> bugs that are fixed in the meantime and works much different in many
>
> aspects than the newer versions. Upgrading later is a pain!

so, which version should I install ?
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Segmentation fault (Core dumped)

Alex Danilo
>so, which version should I install ?

You should build from this one:

https://ftp.mozilla.org/pub/mozilla.org/js/mozjs-24.2.0.tar.bz2
SHA1: ce779081cc11bd0c871c6f303fc4a0091cf4fe66

Which is the latest official release (24) that Sean Stangl from Mozilla published recently.

Alex

_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine