我正在尝试在Ubuntu 17.04 x64下使用SpiderMonkey。

SpiderMonkey项目没有错误跟踪程序,在经过严格的谷歌搜索之后,我还没有发现有关我的问题的任何解决方法,因此我向社区寻求帮助。

1)从官方download page下载的SpiderMonkey源代码:
https://people.mozilla.org/~sfink/mozjs-45.0.2.tar.bz2
2)生成命令(来自官方documentation page):

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

3)也从官方example page嵌入mozjs库的示例代码,但是在这里,我带来了最简单的代码,该代码再现了错误:
#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)我已经使用此命令进行了编译,也在官方示例页面上给出了该命令:
$ 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)运行后,在JS_NewContext调用中出现了Segmentation Fault,具有以下回溯:
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)最后,有关版本的一些信息:
$ 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

因为代码非常简单,所以我认为环境有问题,但是我不知道原因。

因此,也许您遇到了相同的问题,并且知道解决该问题的解决方法,请帮助我进行处理。

谢谢 :)

最佳答案

这是个老问题:MOZ_GLUE_IN_PROGRAM破坏了独立的版本(最后在Linux平台上)。
要禁用MOZ_GLUE_IN_PROGRAM,请编辑js/src/configure.in并替换

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

通过
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

有关详细信息,请参见Bugzilla Bug 1176787

关于c++ - 在Ubuntu 17.04 x64下使用SpiderMonkey(libmozjs-45.0.2)进行段错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44575115/

10-12 14:19