Search code examples
c++linux64-bitsegmentation-faultspidermonkey

Segmentation Fault using SpiderMonkey (libmozjs-45.0.2) under Ubuntu 17.04 x64


I'm trying to use SpiderMonkey under Ubuntu 17.04 x64.

The SpiderMonkey project has no bug tracker, also after diligently googling I had not found any workaround about my problem, so I ask the community for help.

1) SpiderMonkey source code downloaded from official download page:

https://people.mozilla.org/~sfink/mozjs-45.0.2.tar.bz2

2) Command for build (from official documentation page):

$ cd js/src
$ autoconf2.13
$ mkdir build_OPT.OBJ
$ cd build_OPT.OBJ
$ ../configure
$ make

3) Example code for embedding the mozjs library also from official example page, but here I bring the simplest code, which reproduce the error:

#include "jsapi.h"
#include "js/Initialization.h"

int main(int argc, const char *argv[])
{
    JS_Init();

    JSRuntime *rt = JS_NewRuntime(8L * 1024 * 1024);
    if (!rt)
        return 1;

    JSContext *cx = JS_NewContext(rt, 8192);
    if (!cx)
        return 1;

    // here I plan to evaluate JavaScript expressions...

    JS_DestroyContext(cx);
    JS_DestroyRuntime(rt);
    JS_ShutDown();
    return 0;
}

4) I have compiled with this command, also given at official examples page:

$ g++ -std=c++11 -I~/mozjs-45.0.2/js/src/build_OPT.OBJ/dist/include -L~/mozjs-45.0.2/js/src/build_OPT.OBJ/dist/bin main.cpp -o main -lmozjs-45 -lz -lpthread -ldl

5) After run, got Segmentation Fault at JS_NewContext call, with following backtrace:

Starting program: /home/tumick/C/cpp/main 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff5600700 (LWP 12326)]
[New Thread 0x7ffff53ff700 (LWP 12327)]
[New Thread 0x7ffff51fe700 (LWP 12328)]
[New Thread 0x7ffff4ffd700 (LWP 12329)]
[New Thread 0x7ffff4dfc700 (LWP 12330)]

Thread 1 "main" received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
#0  0x0000000000000000 in ?? ()
#1  0x00007ffff6d62722 in js::ScriptBytecodeHasher::hash (l=<synthetic pointer>..., l=<synthetic pointer>...) 
    at /home/tumick/mozjs-45.0.2/js/src/jsscript.h:2411
#2  js::detail::HashTable<js::SharedScriptData* const, js::HashSet<js::SharedScriptData*, js::ScriptBytecodeHasher, js::SystemAllocPolicy>::SetOps, js::SystemAllocPolicy>::prepareHash (l=<synthetic pointer>...) at ../../dist/include/js/HashTable.h:1126
#3  js::detail::HashTable<js::SharedScriptData* const, js::HashSet<js::SharedScriptData*, js::ScriptBytecodeHasher, js::SystemAllocPolicy>::SetOps, js::SystemAllocPolicy>::lookupForAdd (l=<synthetic pointer>..., this=<optimized out>) at ../../dist/include/js/HashTable.h:1638
#4  js::HashSet<js::SharedScriptData*, js::ScriptBytecodeHasher, js::SystemAllocPolicy>::lookupForAdd (l=<synthetic pointer>..., 
this=<optimized out>) at ../../dist/include/js/HashTable.h:386
#5  SaveSharedScriptData (cx=cx@entry=0x5555557841f0, script=..., ssd=<optimized out>, nsrcnotes=1)
    at /home/tumick/mozjs-45.0.2/js/src/jsscript.cpp:2502
#6  0x00007ffff6d62c41 in JSScript::fullyInitTrivial (cx=cx@entry=0x5555557841f0, script=..., script@entry=...)
    at /home/tumick/mozjs-45.0.2/js/src/jsscript.cpp:2886
#7  0x00007ffff6d20316 in CreateFunctionPrototype (cx=0x5555557841f0, key=<optimized out>) at /home/tumick/mozjs-45.0.2/js/src/jsfun.cpp:785
#8  0x00007ffff6ddcb52 in js::GlobalObject::resolveConstructor (cx=0x5555557841f0, global=..., key=<optimized out>)
    at /home/tumick/mozjs-45.0.2/js/src/vm/GlobalObject.cpp:158
#9  0x00007ffff6ddd06e in js::GlobalObject::ensureConstructor (cx=cx@entry=0x5555557841f0, global=..., global@entry=..., 
key=key@entry=JSProto_Function) at /home/tumick/mozjs-45.0.2/js/src/vm/GlobalObject.cpp:98
#10 0x00007ffff6c4fe8b in CreateObjectConstructor (cx=0x5555557841f0, key=<optimized out>)
    at /home/tumick/mozjs-45.0.2/js/src/builtin/Object.cpp:1035
#11 0x00007ffff6ddcbbe in js::GlobalObject::resolveConstructor (cx=0x5555557841f0, global=..., key=<optimized out>)
    at /home/tumick/mozjs-45.0.2/js/src/vm/GlobalObject.cpp:166
#12 0x00007ffff6ddd06e in js::GlobalObject::ensureConstructor (cx=cx@entry=0x5555557841f0, global=..., global@entry=..., 
    key=key@entry=JSProto_Object) at /home/tumick/mozjs-45.0.2/js/src/vm/GlobalObject.cpp:98
#13 0x00007ffff6a05f92 in js::GlobalObject::getOrCreateObjectPrototype (cx=0x5555557841f0, this=<optimized out>)
    at /home/tumick/mozjs-45.0.2/js/src/vm/GlobalObject.h:342
#14 CreateArrayPrototype (cx=0x5555557841f0, key=<optimized out>) at /home/tumick/mozjs-45.0.2/js/src/jsarray.cpp:3235
#15 0x00007ffff6ddebe1 in InitBareBuiltinCtor (cx=cx@entry=0x5555557841f0, global=..., global@entry=..., 
    protoKey=protoKey@entry=JSProto_Array) at /home/tumick/mozjs-45.0.2/js/src/vm/GlobalObject.cpp:368
#16 0x00007ffff6ddee10 in js::GlobalObject::initSelfHostingBuiltins (cx=cx@entry=0x5555557841f0, global=global@entry=..., 
    builtins=builtins@entry=0x7ffff7da9520 <intrinsic_functions>) at /home/tumick/mozjs-45.0.2/js/src/vm/GlobalObject.cpp:413
#17 0x00007ffff6e6ffcb in JSRuntime::createSelfHostingGlobal (cx=cx@entry=0x5555557841f0)
    at /home/tumick/mozjs-45.0.2/js/src/vm/SelfHosting.cpp:1730
#18 0x00007ffff6e70115 in JSRuntime::initSelfHosting (this=this@entry=0x5555557693f0, cx=cx@entry=0x5555557841f0)
    at /home/tumick/mozjs-45.0.2/js/src/vm/SelfHosting.cpp:1754
#19 0x00007ffff6cd3e23 in js::NewContext (rt=0x5555557693f0, stackChunkSize=<optimized out>)
    at /home/tumick/mozjs-45.0.2/js/src/jscntxt.cpp:122
#20 0x000055555555499b in main ()

6) Finally, some information about versions:

$ uname -r 
4.10.0-20-generic 
$ gcc --version 
gcc (Ubuntu 6.3.0-12ubuntu2) 6.3.0 20170406 
$ ld --version 
GNU lg (GNU Binutils for Ubuntu) 2.28

Because the code is extremely simple, I suppose something's wrong with my environment, but I have no idea about the reason.

So, maybe You've meet the same problem and know the workaround to solve it, please, help me to deal with it.

Thank You :)


Solution

  • This is old issue: MOZ_GLUE_IN_PROGRAM breaks stand-alone builds(at last on Linux platform). To disable MOZ_GLUE_IN_PROGRAM edit js/src/configure.in and replace

    case "${OS_TARGET}" in
    Android|WINNT|Darwin)
      MOZ_GLUE_IN_PROGRAM=
      ;;
    *)
      dnl On !Android !Windows !OSX, we only want to link executables against   mozglue
      MOZ_GLUE_IN_PROGRAM=1
      AC_DEFINE(MOZ_GLUE_IN_PROGRAM)
    ;;
    esac
    

    By

    dnl In stand-alone builds we always only want to link executables against mozglue.
    if test "$JS_STANDALONE"; then
      MOZ_GLUE_IN_PROGRAM=
    else
      case "${OS_TARGET}" in
      Android|WINNT|Darwin)
        MOZ_GLUE_IN_PROGRAM=
        ;;
      *)
        dnl On !Android !Windows !OSX, we only want to link executables against      mozglue
        MOZ_GLUE_IN_PROGRAM=1
        AC_DEFINE(MOZ_GLUE_IN_PROGRAM)
      ;;
      esac
    fi
    

    See Bugzilla Bug 1176787 for details.